• Codeforces Round #572 (Div. 2)


    A. Keanu Reeves

    所有“字符串”均为0,1组成。定义好串:0和1字符数不相等 坏串:0和1字符数相等

    给一个字符串 问:最小拆分分成几个字符串可以全部变成好串

    解法:字符串有可能0,1的数量不相等,本来就是好串。直接输出1和原字符串。字符串0,1数量也可能相等,是坏串。我们只需单独把字符串第一个字符和剩余字符串分别输出。两个必定都是好串。此时拆分成了两个子字符串,故只有1和2两种情况。

    #include <iostream>
    using namespace std;
    int n;
    char a[1010];
    int temp;
    int main()
    {
        scanf("%d",&n);
        scanf("%s",a);
        for(int i=0;i<n;i++)
        {
            if(a[i]-'0'==1)
            temp++;
            if(a[i]-'0'==0)
            temp--;
        }
        if(temp!=0)
        {
            printf("1
    ");
        for(int i=0;i<n;i++)
        {
        printf("%d",a[i]-'0');
        }
    }
        if(temp==0)
        {
            printf("2
    ");
            printf("%d ",a[0]-'0');
            for(int i=1;i<n;i++)
            printf("%d",a[i]-'0');
        }
    printf("
    ");
    }
    View Code

    B. Number Circle

     给定一个数组里面有若干元素,想把它连成一个环。问能否连成一个 环种所有元素满足a[i]<a[i-1]+a[i+1]关系,即中间元素小于两边元素的和。

    分析:很显然 对数组排序后数组从大到小有序 若满足 a[n]<a[n-1]+a[n-2]则环必定存在,剩下考虑环的顺序。由样例启发可知 最大的三个数必定是 a[i-1]  a[n]  a[i-2]排列,这样就满足了题墓里给的要求,剩下的 0~n-3的元素只需要顺序排列  由图中图一易知  4比 1大 ,5比4大...所有元素拜访均满足要求

    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int N =100100;
    int a[N];
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
        sort(a+1,a+1+n);
        if(a[n-2]+a[n-1]<=a[n])
        printf("NO
    ");
        else
        {
            printf("YES
    ");
            if(n>3)
            {
            printf("%d %d %d %d",a[1],a[n-2],a[n],a[n-1]);
            for(int i=n-3;i>=2;i--)
            printf(" %d",a[i]);
        }
        else
        {
            for(int i=1;i<=n;i++)
            printf("%d ",a[i]);
        }
            printf("
    ");
        }
    }
    View Code

    C. Candies!

    简单前缀和

    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int N=100010;
    int sum[N];
    int temp;
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&temp);
            sum[i]=temp+sum[i-1];
        }
        int m;
        scanf("%d",&m);
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            int ans=(sum[b]-sum[a-1])/10;
            printf("%d
    ",ans);
        }
    }
    View Code

    E.Count Pairs

     式子两边同时乘(a[i]-a[j])

    两次平方差公式再移项可得 a[i]^4-k*a[i]%p=a[j]^4-k*a[j]%p

    直接遍历n 求出对于数组每一项元素代入上式的结果,如果出现相同的就进行累加。最后得出答案。

    #include <iostream>
    #include <algorithm>
    #include <map>
    using namespace std;
    typedef long long ll;
    map<ll,ll>mp;
    int main()
    {
        ll n,p,k;
        scanf("%lld%lld%lld",&n,&p,&k);
        ll count=0;
        for(int i=1;i<=n;i++){
            ll x;
            scanf("%d",&x);
            ll num=(x*x%p*x%p*x%p-(k*x%p+p+p))%p;//多加p防止做差时出现负数
            count+=mp[num];
            mp[num]++; 
        }
        printf("%lld
    ",count);
    }
    View Code
  • 相关阅读:
    ajax中的application/x-www-form-urlencoded中的使用
    XMLHttpRequest的POST中文表单问题解决方案
    Address already in use: JVM_Bind<null>:80
    javascript-XMLHttpRequest
    The web application [/codeMarket] registered the JBDC driver[.........] but failed to unregister it when the web application was stopped. To prevent
    cookie的一些细节
    js操作cookie
    javascript与服务器3
    javascript与服务器1
    javascript与服务器2
  • 原文地址:https://www.cnblogs.com/daoyuan/p/12423669.html
Copyright © 2020-2023  润新知