Codeforces Round #452 (Div. 2) A B C

Codeforces Round #452 (Div. 2)

A Splitting in Teams

题目链接:

http://codeforces.com/contest/899/problem/A

思路:

统计1和2出现的次数,尽量使2能够与1匹配尽可能多用。假设1再匹配完2之后还有剩余,则求出3个1可组成的方案

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200005;
ll num,cnt[3];
int main() {
    ll n;
    scanf("%I64d",&n);
    for(ll i=0;i<n;++i) {
        scanf("%I64d",&num);
        cnt[num]++;
    }
    ll res=min(cnt[1],cnt[2]);
    printf("%I64d\n",res+(cnt[1]-res)/3);
    return 0;
}

B Proper Nutrition

题目链接:

http://codeforces.com/contest/899/problem/B

思路:

暴力枚举4种情况,24可以跨越三年,谨记...

代码:

#include <bits/stdc++.h>
using namespace std;
int b[4][36]={
    {31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31},
    {31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31},
    {31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31},
    {31,29,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31}
};
int a[24];
int main() {
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i) scanf("%d",&a[i]);
    int flag=0;
    for(int i=0;i<4;++i) {
        for(int j=0;j<36;++j) {
            int l=j,r=0;
            while(a[r]==b[i][l]) {
                ++r;
                ++l;
                if(r==n) break;
            }
            if(r==n) {
                flag=1;
                break;
            }
        }
        if(flag) break;
    }
    if(flag) printf("YES\n");
    else printf("NO\n");
    return 0;
}

C Phone Numbers

题目链接:

http://codeforces.com/contest/899/problem/C

思路:

我们尽可能让两组都尽可能趋近于sum/2。因为给出的n个数字是从1到n连续的,所以利用这个性质,假定其中较小的一组必定是连续的,标记为1。输出没有被标记的即可。特判一下n=2的情况。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[60005];
int main() {
    ll n;
    ll dis=0;
    ll cnt=0;
    scanf("%I64d",&n);
    if(n==2) {
        printf("1\n");
        printf("1 1\n");
        return 0;
    }
    memset(a,0,sizeof(a));
    ll sum=(1+n)*n/2;
    sum/=2;
    for(ll i=1;i<=n;++i) {
        if(sum%(i+i+1)==0) {
            ll temp=sum/(i+i+1);
            if(temp<=i) {
                cnt=n-temp*2;
                for(ll j=i-temp+1;j<=i+temp;++j) {
                    a[j]=1;
                    dis+=j;
                }
                break;
            }
        }
    }
    printf("%I64d\n",abs((1+n)*n/2-dis-dis));
    printf("%I64d ",cnt);
    int index=0;
    for(int i=1;i<=n;++i) {
        if(a[i]==0) {
            if(index==0) printf("%d",i);
            else printf(" %d",i);
            ++index;
        }
    }
    printf("\n");
    return 0;
}
posted @ 2017-12-17 21:18  lemonsbiscuit  阅读(289)  评论(0编辑  收藏  举报