ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1706C - Qpwoeirut And The City

2022-08-26 10:00:12  阅读:161  来源: 互联网

标签:1706C City return int max tot 补数 include Qpwoeirut


/*
 *                                |~~~~~~~|
 *                                |       |
 *                                |       |
 *                                |       |
 *                                |       |
 *                                |       |
 *     |~.\\\_\~~~~~~~~~~~~~~xx~~~         ~~~~~~~~~~~~~~~~~~~~~/_//;~|
 *     |  \  o \_         ,XXXXX),                         _..-~ o /  |
 *     |    ~~\  ~-.     XXXXX`)))),                 _.--~~   .-~~~   |
 *      ~~~~~~~`\   ~\~~~XXX' _/ ';))     |~~~~~~..-~     _.-~ ~~~~~~~
 *               `\   ~~--`_\~\, ;;;\)__.---.~~~      _.-~
 *                 ~-.       `:;;/;; \          _..-~~
 *                    ~-._      `''        /-~-~
 *                        `\              /  /
 *                          |         ,   | |
 *                           |  '        /  |
 *                            \/;          |
 *                             ;;          |
 *                             `;   .       |
 *                             |~~~-----.....|
 *                            | \             \
 *                           | /\~~--...__    |
 *                           (|  `\       __-\|
 *                           ||    \_   /~    |
 *                           |)     \~-'      |
 *                            |      | \      '
 *                            |      |  \    :
 *                             \     |  |    |
 *                              |    )  (    )
 *                               \  /;  /\  |
 *                               |    |/   |
 *                               |    |   |
 *                                \  .'  ||
 *                                |  |  | |
 *                                (  | |  |
 *                                |   \ \ |
 *                                || o `.)|
 *                                |`\\) |
 *                                |       |
 *                                |       |
 */
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<map>
//#pragma GCC optimize(3)
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define scan(x)   scanf("%lld",&x)
// #define int long long
#define lowbit(x) x&(-x) //二进制最低位所代表的数
#define PI 3.1415926535
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int _read(){
    char ch=nc();int sum=0;
    while(!(ch>='0'&&ch<='9'))ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum;
}
typedef pair<int,int> PII;
typedef long long ll;
int gcd(int a,int b){
    return b>0 ? gcd(b,a%b):a;
}
int exgcd(int a,int b,int &x,int &y)
{
    if(!b)
    {
        x = 1,y = 0;
        return a;
    }
    int d = exgcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}
const int N = 1e5+10;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
void init()
{
    cin.tie(0),cout.tie(0);
    ios::sync_with_stdio(false);
}

int a[N];

int get(int i)
{
    return max(0,max(a[i-1],a[i+1])-a[i]+1);
}

void solve()
{
    int n;
    cin>>n;
    ll tot = 0,ans;
    for(int i=1;i<=n;i++)cin>>a[i];
    if(n%2)
    {
        for(int i=2;i<n;i+=2)
        {
            tot += get(i);
        }
        cout<<tot<<endl;
    }
    else
    {
        for(int i=2;i<n;i+=2)
        {
            tot += get(i);
        }
        ans = tot;
        for(int i=n-1;i>=2;i-=2)
        {
            tot -= get(i-1);
            tot += get(i);
            ans = min(ans,tot);
        }
        // for(int i=2;i<n;i+=2)
        // {
            // tot -= get(i);
            // tot += get(i+1);
            // ans=min(ans,tot);
        // }
        cout<<ans<<endl;
    }
}

signed main()
{
    init();
    int t;
    cin>>t;
    while(t--)solve();
}
View Code

Problem - 1706C - Codeforces

思路:

找峰,并尝试补峰,找出补得最少,但数量最多的做法,并输出补的数量(两边不作峰)。

峰对奇偶数敏感,因为峰是三个数字中出一个。

所以对于奇数,没有特殊的排列:

1 0 1

1 0 1 0 1

0 1 0 1 0

所以直接判断偶数下标的元素是否要补数即可max(0,max(a[i-1],a[i+1])-a[i]+1)

对于偶数,有几种排列,但是遵循一个原则,元素是相隔的:

0 1 0 1 0 1 0 0

0 1 0 1 0 0 1 0

0 1 0 0 1 0 1 0

0 0 1 0 1 0 1 0

有一个非常巧妙的做法,就是从后往前遍历,减去原本偶数位所加上的补数,然后再减去奇数位的补数,每一次加减判断最小方案,最后输出。

tips:补数为max(0,max(a[i-1],a[i+1])-a[i]+1)

 

标签:1706C,City,return,int,max,tot,补数,include,Qpwoeirut
来源: https://www.cnblogs.com/yeonnyuui/p/16626580.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有