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\n",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\n",ans);
    }
    return 0;
}



發佈了249 篇原創文章 · 獲贊 7 · 訪問量 56萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章