Codeforces 779

Codeforces 779

Codeforces 779B Weird Rounding

貪心,從後面往前面貪心,注意特判即可

#include <cstdio>
#include <cmath>

using namespace std;

typedef long long LL;

LL n,k,ans,cnt,tmp;

int main()
{
    scanf("%lld%lld",&n,&k);
    if(n==0)    {puts("0");return 0;}
    tmp=n;
    while(n)
    {
        if(n%10==0) cnt++;
        else ans++;
        n/=10;
        if(cnt==k)  break;
    }
    printf("%lld\n",cnt==k?ans:(int)log10(tmp));
    return 0;
}

Codeforces 779C Dishonest Sellers

可以排序,也可以先O(n) 掃描一遍,同一天選較少的,新建數組儲存差價,如果不夠k,再補差價即可
貌似可以出到1e7 不能毒害

#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

const int MAXN=200000+10;

LL n,k;

LL l[MAXN],a[MAXN],cnt,ans,tmp[MAXN];

bool cmp(int A,int B)   {return A<B;}

int main()
{
    scanf("%lld%lld",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%lld",&l[i]);
    for(int j=1;j<=n;j++)
        scanf("%lld",&a[j]);
    for(int i=1;i<=n;i++)
    {
        if(l[i]<=a[i])  ans+=l[i],cnt++;
        else ans+=a[i],tmp[++tmp[0]]=l[i]-a[i];
    }
    sort(tmp+1,tmp+tmp[0]+1,cmp);
    for(int i=1;i<=(k-cnt);i++)
        ans=ans+tmp[i];
    printf("%lld\n",ans);
    return  0;
}

Codeforces 779D String Game

二分答案,check即可

#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN=200000+10;

char a[MAXN],b[MAXN];
int pos,ans,len1,len2;
int w[MAXN];
bool v[MAXN];

bool check(int m)
{
    int cnt=0;
    memset(v,0,sizeof v);
    for(int i=1;i<=m;i++)
        v[w[i]-1]=1;
    for(int i=0;i<len1;i++)
    {
        if(b[cnt]==a[i]&&(!v[i]))   cnt++;
        if(cnt==len2)   return true;
    }
    if(cnt==len2)   return true;
    return false;

}

int main()
{
    scanf("%s",a);
    scanf("%s",b);
    len1=strlen(a),len2=strlen(b);
    for(int i=1;i<=len1;i++)
        scanf("%d",&w[i]);
    int l=0,r=len1;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(check(mid))  ans=mid,l=mid+1;
        else r=mid-1;
    }
    printf("%d\n",ans);
}

Codeforces 779E Bitwise Formula

二進制分開枚舉即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章