CSU 1900: 锋芒不露 1901: 赏赐 OR 灾难 1904: 精灵的交际网

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nameofcsdn/article/details/80317276

1900: 锋芒不露

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
int L[105],R[105];

int main()
{
    int n;
    int a,b;
    int Max;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        memset(L,0,sizeof(L));
        memset(R,0,sizeof(R));
        int lm=100,rm=0;
        for(int k=1;k<=n;k++)
        {
            scanf("%d%d",&a,&b);
            L[a]++;R[b]++;
            if(lm>a) lm=a;
            if(rm<b) rm=b;
            Max=0;
            int i=lm,j=rm;
            int p=L[lm],q=R[rm];
            int match=0;
            while(match!=k)
            {
                if(p==0) {p=L[++i];continue;}
                if(q==0) {q=R[--j];continue;}
                if((i+j)>Max) Max=i+j;
                if(p==q)
                {
                    match+=p;
                    i++;j--;
                    p=L[i];q=R[j];
                }else if(p<q)
                {
                    q-=p;
                    match+=p;
                    p=L[++i];
                }else if(p>q)
                {
                    p-=q;
                    match+=q;
                    q=R[--j];
                }
            }
            printf("%d\n",Max);
        }
        printf("\n");
    }
    return 0;
}

1901: 赏赐 OR 灾难

#include<iostream>
#include<string>
#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
const int MAXN=100005;
const int INF=1000000000;
int Min[MAXN];//Min[i]表示前i个宝物中的最小价值
int w[MAXN];
stack<int> s;//单调栈

int main()
{
    ios::sync_with_stdio(false);
    int n;
    while(cin>>n)
    {
        while(!s.empty()) s.pop();
        for(int i=1;i<=n;i++)
        {
            cin>>w[i];
            if(i==1) Min[i]=w[i];
            else Min[i]=min(Min[i-1],w[i]);
        }
        bool flag=false;
        s.push(w[n]);
        for(int i=n-1;i>=2;i--)
        {
            int a=Min[i-1];//i左边的最小值
            int c=-1;
            while(!s.empty()&&s.top()<w[i])
            {
                c=s.top();
                s.pop();
            }
            s.push(w[i]);
            if(c>a)
            {
                flag=true;
                break;
            }
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

1904: 精灵的交际网

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=50005;
int fa1[maxn],rel[maxn];
int find1(int x)
{
    if(fa1[x]==x)
      return fa1[x];
    int tem=fa1[x];
    fa1[x]=find1(fa1[x]);
    rel[x]=(rel[x]+rel[tem])%2;
    return fa1[x];
}

void merge1(int x,int y)
{
    int fx=find1(x),fy=find1(y);
        fa1[fx]=fy;
      rel[fx]=(rel[y]-rel[x]+1)%2;
}

int main()
{
    int T,cnt=0;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,flag1=-1,flag2=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
           {
               fa1[i]=i;
               rel[i]=0;
           }
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(flag2==1)
                continue;
            if(find1(a)==find1(b))
            {
                if(rel[a]==rel[b]&&flag2==0)
                {
                    flag2++;
                    flag1=i+1;
                }
            }
            else
                merge1(a,b);
        }
       printf("%d\n",flag1);
    }
    return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页