Codeforces Round #491 (Div. 2) 题解

2019-05-09   19:45:34

题目链接:https://codeforces.com/contest/991

官方题解:https://codeforces.com/blog/entry/60181

第一题(水):

A:餐   B:餐    C:AB都去  N:总人数

问:有多少人没去,若数据错误输出-1

思路:判断A+B-C是否大于总人数(即N)

   判断两个都去的(即C)是否大于只一个的(即AorB)

代码:

#include<cstdio>
#include<iostream>
#include<algorithm> 
const int maxn=110;
using namespace std;
int main()
{
    int a,b,c,n;
    cin>>a>>b>>c>>n;
    int x=a+b-c;
    if((x>=n)||a<c||b<c)
    printf("-1\n");
    else
    {
        printf("%d\n",n-x);
    }
    return 0;
 } 

第二题(水):

问:怎样使平均分大于4.5且变化次数最少

思路:sort后从小到大向后依次变为5直到均值大于4.5,因为增加的值多所以所需次数最少

代码:

#include<cstdio>
#include<iostream>
#include<algorithm> 
const int maxn=110;
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[maxn];
    int sum=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        sum+=a[i];
    }
    sort(a,a+n);
    int x=sum*10;  //浮点数不能比较,化为整数
    int z=45*n;
    int cnt=0;
    if(x>=z)
    printf("0\n");
    else
    {
        for(int i=0;i<n;i++)
        {
            if(x<z&&a[i]<5)  //从最小值向上迭代
            {
                cnt++;
                x+=(5-a[i])*10;  //加的时候减去原始值
                if(x>=z)
                break;
            }
         } 
        printf("%d\n",cnt);
    }
    return 0;
 } 

第三题(:

问:A吃固定值k,B吃10%,求使A吃的糖大于一半的最小值k

思路:二分求最小

关于二分

代码:

#include<iostream> 
#include<algorithm>
typedef long long ll;
using namespace std;
ll n;
int check(ll x)  //检查k是否满足使A吃的大于1/2
{
    ll now=n,sum=0;
    while(now>0)
    {
        ll k=min(x,now);
        sum+=k;
        now-=k;
        now-=now/10;
    }
    return sum*2>=n;  //1/0
}
int main()
{
    cin>>n;
    ll l=1;
    ll r=n;
    ll mid;
    ll flag=n;
    while(l<=r)  //二分模板
    {
        mid=(l+r)/2;
        if(check(mid))
        {
            flag=mid;
            r=mid-1;
        }
        else
        {
            l=mid+1;
        }
    }
    cout<<flag<<endl;
    return 0;
}

第四题 : )

猜你喜欢

转载自www.cnblogs.com/BYBL/p/10840630.html