Codeforces Round #316 (Div. 2)



A题


题目大意:

     一个城市有m个城市,每一个城市先投票选出一个候选人,得票多的胜出,得票同样时,下标小的胜出,接着在城市

选出的候选人中选出一个胜者。


解题思路:

      依照题意模拟就好了。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=15000;
int d[maxn];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int maxx=-1,maxi=0;
        int x;
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&x);
            if(x>maxx)
            {
                maxx=x;
                maxi=j;
            }
        }
        //cout<<maxi<<endl;
        d[maxi]++;
    }
    int ans=1;
    for(int i=1;i<=n;i++)
    {
        if(d[i]>d[ans])
        {
           ans=i;
        }
    }
    printf("%d
",ans);
    return 0;
}


B题


题目大意:

      两人在1~n内任选出一个数,再随机产生一个1~n的数,两人谁的离这个数相近就胜出,如今一人已经选了m,且两人距离同样时这人胜出,求还有一个人选哪个数有最大的概率胜出。


解题思路:

     假设m>n/2,肯定选m-1,假设m<n/2,肯定选m+1,n=1时要特殊处理。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    int n,m,ans;
    scanf("%d%d",&n,&m);
    if(m>n/2)
    ans=m-1;
    else
    ans=m+1;
    if(n==1)
    ans=1;
    cout<<ans<<endl;
    return 0;
}



C. Replacement


题目大意:

       事实上就是统计每次改变字符串内的一个元素,还有多少个不同的子串".."。


解题思路:

      先统计有多少个"..",接着进行代替操作,这是有2种情况,'.'代替字母。字母代替'.',不管哪种情况,我们仅仅需

 考虑操作的左边与右边的情况,拿'.'代替字母的情况,假设'.'的左边是'.‘,则多了1种'..'的子串。假设右边也是'.'。也

  多了一种。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=300000+1000;
char s[maxn];
int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    scanf("%s",s+1);
    int ans=0;
    int cur=0;
    for(int i=1;i<=n;i++)
    {
        if(s[i]=='.')
        {
            cur++;
        }
        else
        {
            if(cur>0)
            {
                ans+=(cur-1);
            }
            cur=0;
        }
    }
    if(cur>0)
    ans+=(cur-1);
    for(int i=0;i<q;i++)
    {
        int x;
        char op[5];
        scanf("%d%s",&x,op);
        if(op[0]=='.')
        {
            if(s[x]!='.')
            {
                int temp=0;
                if(x>1)
                {
                    if(s[x-1]=='.')
                    temp++;
                }
                if(x<n)
                {
                    if(s[x+1]=='.')
                    temp++;
                }
                ans+=temp;
                s[x]='.';
            }
        }
        else
        {
            if(s[x]=='.')
            {
                int temp=0;
                if(x>1)
                {
                    if(s[x-1]=='.')
                    temp++;
                }
                if(x<n)
                {
                    if(s[x+1]=='.')
                    temp++;
                }
                ans-=temp;
                s[x]=op[0];
            }
        }
        printf("%d
",ans);
    }
    return 0;
}



原文地址:https://www.cnblogs.com/jzssuanfa/p/7086820.html