CF Gym 100637F The Pool for Lucky Ones

题意:给你一串非负整数,可以将一个非零数减1,加到相邻的数字上,要使其中所有最大数字的和最小。

题解:模拟可以过。也可以分析,可以要减少最大数字和,如果最大数字出现大于等于3次,可以把最大数字加一,或者把某个最大数字减一,最大数字出现减少一次。但是药品注意一些特殊情况,下面详述。

先扫一遍,如果最大数字为0,那么输出0,如果最大数字为1,那么如果可以合并成2的话,那么就输出2,否则输出数字和sum。否则,看最大数字出现次数,如果为一次,那么检查,周围是否有比它小3的数字,(如果只是比它小2的话,那么操作完以后至少会出现两个次大的数字),如果有还有检查一下操作前是否有次大的数字,然后根据检查情况输出Max或Max-1;如果出现两次,检查两个最大数字周围有没有比它小2的数字,如果有输出Max,没有的话输出Max+1,(Max>=2且不满足前者,周围一定有数字非零);对于次数大于等于3次的,检查最大数字周围有没有非零数字,如果有输出Max+1,否则sum-Max。

#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<algorithm>

using namespace std;

//#define local
typedef long long ll;
const int maxn = 1e5+5;
int p[maxn];
int main()
{
#ifdef local
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
#endif // local
    int n;
    scanf("%d",&n);
    ll Max = 0;
    int pos = -1;
    for(int i = 0; i < n ;i++){
        scanf("%d",p+i);
        if(p[i]>Max){
            Max = p[i];pos =  i;
        }
    }
    if(Max == 0){ printf("0"); return 0;}
    if(Max == 1){
        int sum = p[0];
        bool canMerge = false;
        for(int i = 1; i < n; i++){
            sum += p[i];
            if(p[i]&&p[i-1]) { canMerge = true; break;  }
        }
        if(canMerge) printf("2");
        else printf("%d",sum);
        return 0;
    }
    long long sum = 0;
    bool canMerge = false;
    bool lessone = false;

    for(int i = pos; i < n; i++){
        if(p[i] == Max) sum += Max;
    }
    if(sum == Max){
        if(pos>0) { if(p[pos-1]<=p[pos]-3)lessone = true; }
        if(pos<n-1) { if(p[pos+1]<=p[pos]-3)lessone = true; }
        if(lessone) {
            for(int i = 0; i < n; i++){
                if(p[i] == Max-1) {
                    printf("%d",Max);
                    return 0;
                }
            }
            printf("%d",Max-1);
        }
        else printf("%d",Max);
        return 0;
    }
    if(sum == Max*2){ //keng
        for(int i = pos; i < n; i++){
            if(p[i] == Max){
                if((i>0&& p[i-1]<= Max-2 )|| (i<n-1&& p[i+1] <= Max-2 ) ){
                     printf("%d",Max); return 0;
                }
            }
        }
        printf("%d",Max+1);
    }

    for(int i = pos; i < n; i++){
        if(p[i] == Max){
            if((i>0&& p[i-1])|| (i<n-1&& p[i+1] ) )
                { printf("%d",Max+1); return 0; }
        }
    }

    printf("%I64d",sum-Max);
    return 0;
}
时间: 2024-11-07 05:01:41

CF Gym 100637F The Pool for Lucky Ones的相关文章

Gym 100637F F. The Pool for Lucky Ones

F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/F Description A new swimming pool has been built in Kazan for the forthcoming Water Sports World Championship. The pool has N lanes. Some

Gym 100637F F. The Pool for Lucky Ones 暴力

F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/F Description A new swimming pool has been built in Kazan for the forthcoming Water Sports World Championship. The pool has N lanes. Some

CF gym 101933 K King&#39;s Colors —— 二项式反演

题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \),设为 \( f(i) \),设恰好 i 种颜色为 \( g(i) \) 那么 \( f(i) = \sum\limits_{j=0}^{i} C_{i}^{j} * g(j) \) 二项式反演得到 \( g(i) = \sum\limits_{j=0}^{k} (-1)^{k-j} * C_{k}

CF Gym 101955G Best ACMer Solves the Hardest Problem

链接:https://codeforces.com/gym/101955/problem/G 题意:在二维平面上四种操作: 1,加一个带权的点: 2,删去一个点: 3,给一个点周围欧几里得距离为sqrt(k)的存在的点点权都加w: 4,查询一个到点欧几里得距离为sqrtk的点权和. x, y<6000, k<1e7, sigma(询问次数)<1e6,time:12s 题解:原本以为是数据结构,发现距离为k的x,y其实不多,直接存vector<pii>dis[maxk]暴力即可

CF Gym 102059E Electronic Circuit (set存图删点)

链接:https://codeforces.com/gym/102059/problem/E 题意:n个点, m条线,问这个电路是否合法,合法:可以确定一个起点和一个终点. 题解:不断的删点,删除度数为2的点,再相连,看最终度数为1的点的个数是否为2.set存图 #include <bits/stdc++.h> using namespace std; const int maxn=3e5+5; set<int> S[maxn]; int n, m; void del_edge()

cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

题目: Description standard input/output As most of you know, the Arab Academy for Science and Technology and Maritime Transport in Alexandria, Egypt, hosts the ACPC Headquarters in the Regional Informatics Center (RIC), and it has been supporting our r

cf gym 100960 G. Youngling Tournament set+树状数组

G. Youngling Tournament time limit per test 2 seconds memory limit per test 256 mebibytes input standard input output standard output Yoda, the Grand Master of the Jedi Order, hit on the idea to hold a tournament among younglings. He has not chosen t

CF Gym 100187A Potion of Immortality

根据兔子试药情况可以缩小范围,如果死了,不在试过的药里面,如果活着,在试过的药里. 最糟的情况: 两个原则 1.能确定药所在的范围的尽量大,2.死得兔子尽量多. 如果当前不知道情况的药n为k的二倍以上,那么基于上面两个原则,试过药的兔子肯定会死. 没死:范围k,损失的兔子0 死了:范围n-k,损失的兔子1 (n>2*k) 设r=n%k,经过上述过程,损失了n/k-1只兔子,转移到了当前状态范围w = k+r, 1.r == 0 那么可以补充一个毒药,变成w=k+1,根据鸽巢原理再死一个就可以确定

CF Gym 100500A Poetry Challenge

题解:暴力dfs,如果有一个选择,能让对手必败,那么就是必胜态,能转移到的状态都是对手的必胜态,或者无法转移,就是必败态. 总算是过了,TLE是因为状态没判重,不过我还是有一点没想明白,为什么状态会出现重复 #include<cstdio> #include<cmath> #include<vector> #include<map> #include<set> #include<algorithm> #include<iostr