【Codeforces】Codeforces Round #491 (Div. 2) (Contest 991)

题目

传送门:QWQ

 

 

A:A - If at first you don't succeed...

 

分析:

按照题意模拟

 

代码:

#include <bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c,n;
    scanf("%d%d%d%d",&a,&b,&c,&n);
    
    int ans=n-a-b+c;
    if(a<c || b<c) ans=-1;
    if(ans>0) printf("%d\n",ans);
    else puts("-1");
    
}
View Code

 

 

 


 

 

 

 

 

 

 

B:B - Getting an A

 

分析:

显然把越小的改成5贡献越大。

 

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=300;
int a[maxn];
int main(){
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    int sum=0,ans=0;for(int i=1;i<=n;i++) sum+=a[i];
    for(int i=1;i<=n;i++){
    //    cout<<(double)(sum*1.0/n)<<endl;
        if((double)(sum*1.0/n)>=4.5) break;
        int k=5-a[i]; ans++;
        sum+=k;
    }
    printf("%d\n",ans);
}
View Code

 

 

 


 

 

 

 

 

 

 

C:C - Candies

 

分析:

大力二分

代码:

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
int ok(ll k){
    ll sum=0,x=n;
    while(x>=0){
        if(x>=k) sum+=k;else sum+=x; x=max(x-k,-1ll); x-=x/10;
    }
    //    printf("----  %lld  %lld\n",k,sum);
    return sum>=(n+1)/2;

}
int main(){
      cin>>n;
    ll l=0,r=n+5;
    while(l+1<r){
        ll mid=l+r>>1;
    //    cout<<l<<"  "<<r<<endl;
        
        if(ok(mid)) r=mid;
        else l=mid;
    }
    cout<<r;
}
View Code

 

 


 

 

 

 

 

 

D:D - Bishwock

 

分析:

按位从从左到右扫过去,能填进去就填进去

 

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=200;
char s[maxn], s2[maxn];
int main(){
    scanf("%s%s",s+1,s2+1);
    int n=strlen(s+1);
    int ans=0;
    s[0]='X'; s2[0]=='X';
    for(int i=1;i<=n;i++){
        if(s[i]=='X') continue;
        if(s2[i]=='0' && s2[i-1]=='0'){
            s2[i]='X'; s2[i-1]='X'; s[i]='X'; ans++;
        //    printf("%d 1\n",i);
            continue;
        }
        if(s2[i]=='0' && s2[i+1]=='0'){
            s2[i]='X'; s2[i+1]='X'; s[i]='X'; ans++;
        //    printf("%d 2\n",i);
            continue;
        }
        if(s[i+1]=='0' && s2[i]=='0'){
            s2[i]='X'; s[i+1]='X'; s[i]='X'; ans++;
        //    printf("%d 3\n",i);
            continue;
        }
        if(s[i+1]=='0' && s2[i+1]=='0'){
            s2[i+1]='X'; s[i+1]='X'; s[i]='X'; ans++;
        //    printf("%d 3\n",i);
            continue;
        }
    }
//    printf("%s\n%s\n",s+1,s2+1);
    printf("%d\n",ans);
    
}
View Code

 

 


 

 

 

 

 

 

E:E - Bus Number

 

分析:

https://blog.csdn.net/ZscDst/article/details/80835561

 官方题解做法。。。。。

 

代码:

//copy from https://blog.csdn.net/ZscDst/article/details/80835561

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[15], vis[15];
ll fac[20];
set<string> s;
void split(string x, int *a)
{
    for (int i = 0; i < 10; i++) a[i] = 0;
    for (char i: x) a[i-'0']++;
}
ll getcount()//去重全排列
{
    ll ans = fac[accumulate(a, a+10, 0)];
    for (int i = 0; i < 10; i++) ans /= fac[a[i]];
    return ans;
}
ll getans(string x)
{
    split(x, a);
    for (int i = 0; i < 10; i++) if (vis[i] && !a[i]) return 0;//比原来少了i数字
    sort(x.begin(), x.end());
    if (s.count(x)) return 0;//x这种子集算过了
    else s.insert(x);

    ll ans = getcount();
    if (a[0]) { a[0]--; ans -= getcount(); }//减去前导0情况
    return ans;
}
int main()
{
    fac[0] = 1; for(int i = 1; i < 20; i++) fac[i] = fac[i-1]*i;//计算阶乘
    string n; cin >> n;
    split(n, vis);
    int k = n.size();
    ll ans = 0;
    for (int i = 1; i < (1<<k); i++)//二进制枚举
    {
        string t;
        for (int j = 0; j < k; j++)
            if (i&(1<<j)) t += n[j];
        ans += getans(t);
    }
    printf("%lld\n",ans);
    return 0;
}
View Code

 

 

posted @ 2018-07-04 23:42  noble_(noblex)  阅读(148)  评论(0编辑  收藏  举报
/* */