Codeforces Round #170 (Div. 2)

A 順時針 逆時針選一個最大的就好

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int d[1001];
int main()
{
    int i,j,k,n,a,b,t;
    cin>>n;
    for(i = 1; i <= n ; i++)
        cin>>d[i];
    cin>>a>>b;
    if(a>b)
    {
        t = a;
        a = b;
        b = t;
    }
    if(a==b)
    {
        cout<<"0\n";
        return 0;
    }
    int s1=0,s2=0;
    for(i = a ; i < b ; i++)
    {
        s1+=d[i];
    }
    for(i = 1 ; i < a ; i++)
    s2+=d[i];
    for(i = b ; i <= n ; i++)
    s2+=d[i];
    cout<<min(s1,s2)<<endl;
    return 0;
}


 

B題 模擬 只是當時有一個地方寫錯了 導致重判的時候掛了 以後要注意啊啊

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
char s[50][50];
int main()
{
    int i,j,k[50],n;
    char *ptr,str[50];
    cin>>n;
    for(i = 1; i <= n ; i++)
    {
        cin>>s[i];
        k[i] = strlen(s[i]);
    }
    str[0] = 'a';
    str[1] = '\0';
    int g=0;
    while(1)
    {
        int f = 0;
        for(i = 1; i <= n ; i++)
        {
            ptr = strstr(s[i],str);
            if(ptr)
            {
               f = 1;
            }
        }
        if(!f)
        break;
        if(str[g]=='z')
        {
            int ff = 0;
            for(i = g-1 ; i >=0 ; i--)
                if(str[i]!='z')
                {
                    str[i]+=1;
                    ff = 1;
                    break;
                }
            str[g] = 'a';
            if(!ff)
            {
               g++;
               str[g] = 'a';
               str[g+1] = '\0';
            }
        }
        else
        {
            str[g] = str[g]+1;
        }
    }
    puts(str);
    return 0;
}


 

C 並查集  很容易看出來 讀完題意就知道是並查集了

#include <iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int nu[110],w[110][110],father[110];
int find(int x)
{
    if(x!=father[x])
    father[x] = find(father[x]);
    return father[x];
}
int main()
{
    int n,m,i,j,k,num=0,a,b,f[110],o,flag=0;
    cin>>n>>m;
    for(i =1; i <= n ; i++)
    {
        cin>>k;
        if(k!=0)
            flag = 1;
        nu[i] = k;
        for(j = 1; j <= k ; j++)
        {
            cin>>w[i][j];
        }
    }
    if(!flag)
    {
        cout<<n<<endl;
        return 0;
    }
    for(i = 1; i <= n ; i++)
        father[i] = i;
    for(i = 1; i <= n ; i++)
    {
        for(j = i+1; j <= n; j++)
        {
            memset(f,0,sizeof(f));
            for(o= 1;o <= nu[i] ; o++)
                f[w[i][o]] = 1;
            for(o = 1; o <= nu[j] ; o++)
                if(f[w[j][o]])
                {
                    int a = find(i);
                    int b = find(j);
                    if(a!=b)
                    {
                        father[a] = b;
                    }
                    break;
                }
        }
    }
    for(i = 1; i <= n ; i++)
    {
        if(father[i]==i)
            num++;
    }
    cout<<num-1<<endl;
    return 0;
}


 

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