And wrong of CF1239B & C & D Programming Task in the Train to Catowice City

CF1239B The World Is Just a Programming Task

Title Description

Define a bracket sequence s is excellent, if and only if it is one of several conditions:
1. | s | = 0
2.s = '(' + t + ')', wherein t is an excellent
3. s = t1 + t2, where t1, t2 are excellent
value for a sequence bracket it as a string loop, cut from a number of positions, the string loop can be cut out.
Given a length \ (n \) parenthesis sequence, you can swap in which brackets the two positions (two positions can be equal), the maximum value and ask the program.
The output of any program are correct. \ (n \ leq 3 \ times 10 ^ 5 \)

answer

I do not know how license

Code

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define view(u,k) for(int k=fir[u];~k;k=nxt[k])
#define LL long long
#define maxn 300007
#define ls (u<<1)
#define rs (u<<1|1)
#define mi (l+r>>1)
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)&&ch!='-')ch=getchar();
    if(ch=='-')f=-1,ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return x*f;
}
void write(int x)
{
    if(x==0){putchar('0'),putchar('\n');return;}
    int f=0;char ch[20];
    if(x<0)putchar('-'),x=-x;
    while(x)ch[++f]=x%10+'0',x/=10;
    while(f)putchar(ch[f--]);
    putchar('\n');
    return;
}
int mn[maxn<<2],num[maxn<<2];
int n,cnt0,maxx,maxi,maxj,tp,stk[maxn],pos[maxn],a[maxn],bac[maxn];
char s[maxn],t[maxn];
void work()
{
    tp=0;int po=0;
    rep(i,1,n)
    {
        if(s[i]=='(')stk[++tp]=i;
        else{if(!tp)po=i;else tp--;}
    }
    if(po)
    {
        int cut=po+1;
        int cntt=0;
        rep(i,cut,n)t[++cntt]=s[i],bac[cntt]=i;
        rep(i,1,cut-1)t[++cntt]=s[i],bac[cntt]=i;
    }
    else {rep(i,1,n)t[i]=s[i],bac[i]=i;}tp=0;
    rep(i,1,n)
    {
        if(t[i]=='(')stk[++tp]=i;
        else pos[i]=stk[tp],tp--;
    }
    return;
}
void pu(int u)
{
    mn[u]=min(mn[ls],mn[rs]),num[u]=0;
    if(mn[u]==mn[ls])num[u]=num[ls];
    if(mn[u]==mn[rs])num[u]+=num[rs];
}
void build(int u,int l,int r)
{
    if(l==r){mn[u]=a[l],num[u]=1;return;}
    build(ls,l,mi),build(rs,mi+1,r),pu(u);
}
void ask(int u,int l,int r,int x,int y,int & ansmn,int & ansnum)
{
    if(x<=l&&r<=y)
    {
        if(mn[u]==ansmn)ansnum+=num[u];
        else if(mn[u]<ansmn)ansmn=mn[u],ansnum=num[u];
        return;
    }
    if(x<=mi)ask(ls,l,mi,x,y,ansmn,ansnum);
    if(y>mi)ask(rs,mi+1,r,x,y,ansmn,ansnum);
    return;
}
int main()
{
    scanf("%d%s",&n,s+1);
    if(n&1){puts("0"),puts("1 1");return 0;}
    rep(i,1,n)if(s[i]=='(')cnt0++;
    if(cnt0!=n/2){puts("0"),puts("1 1");return 0;}
    work();
    rep(i,1,n){a[i]=a[i-1]+(t[i]=='('?1:-1);}
    build(1,1,n);int num0=num[1];
    maxx=num0,maxi=maxj=1;
    rep(i,1,n)if(t[i]==')')
    {
        int aa=2147483647,bb=0,ansnow=0;ask(1,1,n,pos[i],i-1,aa,bb);
        if(aa>2)continue;
        if(aa==2)ansnow=num0+bb;
        else ansnow=bb;
        if(ansnow>maxx)maxx=ansnow,maxi=bac[pos[i]],maxj=bac[i];
    }
    printf("%d\n%d %d",maxx,maxi,maxj);
    return 0;
}

CF1239C Queue in the Train

Title Description

There are train numbered 1, 2, ..., \ (n \) is \ (n \) individuals, the \ (i \) individuals will be the first \ (t_i \) seconds to wake up, they wake up after all I wish to take water, then water per person spends \ (p \) minutes.
At the same time there will only be one person can take water, the water then the process will not be interrupted. Other people will line up to pick up water from the process to pick up the water to the water.
After a man woke up, found only in the number of not less than his people to "being connected to water" or "queued" or "woke up and did not receive water" people, will pick up the water does not exist.
People do not need to go drinking time.
Everyone then finished water demand of time. \ (n \ leq 10 ^ 5 ; q, t \ leq 10 ^ 9 \)

answer

This simulation process.
Everyone woke up to the time of the first keyword, second keyword ordering number.
With maintenance people are queuing queue.
For a small number of keywords roots heap maintenance was awake but did not receive water person.

Code

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define view(u,k) for(int k=fir[u];~k;k=nxt[k])
#define LL long long
#define maxn 100007
#define mp make_pair
#define pii pair<LL ,LL>
#define fi first
#define se second
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)&&ch!='-')ch=getchar();
    if(ch=='-')f=-1,ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return x*f;
}
void write(LL x)
{
    if(x==0){putchar('0'),putchar(' ');return;}
    int f=0;char ch[20];
    if(x<0)putchar('-'),x=-x;
    while(x)ch[++f]=x%10+'0',x/=10;
    while(f)putchar(ch[f--]);
    putchar(' ');
    return;
}
int n,tr[maxn];
LL t[maxn];
LL p,ans[maxn],Q[maxn],hd=1,tl;
pii a[maxn];
priority_queue<int >q;
int lt(int x){return x&(-x);}
void add(int x,int k){for(;x<=n;x+=lt(x))tr[x]+=k;return;}
int ask(int x){int k=0;for(;x;x-=lt(x))k+=tr[x];return k;}
int main()
{
    n=read(),p=read();
    rep(i,1,n)t[i]=read(),a[i].fi=t[i],a[i].se=i;
    sort(a+1,a+n+1);
    LL start=0,tim=0,now=1;
    rep(i,1,n)
    {
        while(now<=n&&a[now].fi<=tim)
        {
            if(a[now].se<start&&!ask(a[now].se))add(a[now].se,1),Q[++tl]=now;
            else q.push(-a[now].se);now++;
        }
        if(hd<=tl)add(a[Q[hd]].se,-1),start=a[Q[hd]].se,hd++;
        else if(!q.empty())start=-q.top(),q.pop();
        else{start=a[now].se,tim=a[now].fi,now++;}
        tim+=p,ans[start]=tim;
    }
    rep(i,1,n)write(ans[i]);
    return 0;
}

CF1239D Catowice City

Title Description

There are number \ (1,2, ..., n \ ) is \ (n \) personal and numbered \ (1,2, ..., n \ ) is \ (n \) cats. The first \ (i \) a person of Understanding \ (i \) No. cats.
There \ (m \) th of the form \ ((x, y) \ ) additional conditions, represents \ (X \) No. of people you know \ (Y \) No. Cat.
Q. Can elect at least one person, at least one cat, so that all people do not know all the cats, and the number of persons + cats = \ (the n-\) . If so, the output of the program.
\ (n, m \ leq 10 ^ 6 \)

answer

If you just select \ (n \) animals so that all elected do not know all the cats, you will find the first \ (i \) number and the first \ (i \) select number of cats and only choose one.
This can be 2-SAT solution: Point \ (I \) represents a group selected \ (I \) No. person, point \ (i '\) represents a group selected \ (I \) No. cats; for the condition \ ((x, y) \) , the need to build side \ (X-> the y-\) , \ (the y-'-> the X-\) , represents a group selected \ (x \) after the number of people must choose \ (y \) No person, choose \ (y \ ) after the cat must choose No. \ (x \) No. cats.
Then consider the condition "at least one person, at least one cat": discover this figure is rather special, the edge between the sides and cats only between man and topology edges formed between people and between cat just edges topology formed opposite to FIG. This corresponds to the same number of points of each strongly connected components and the corresponding number of cats midpoint strongly connected components. That is, selected from a strongly connected components without the side (referred to as S), the force is selected from all points strongly connected component, and are not selected from all the other points strongly connected components of the same animal. For another animal, do not choose to force all the points S, are not selected from all points in S. We found that this configuration must conform to the properties of 2-SAT.
If and only if there is a size of \ (n-\)When the strongly connected component, each for only one animal are strongly connected component can not be configured as described above.

Code

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<set>
#include<stack>
#include<vector>
#include<queue>
#define LL long long 
#define maxn 2000007
#define rep(i,x,y) for(int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(int i=(x);i>=(y);--i)
#define view(u,k) for(int k=fir[u];~k;k=nxt[k])
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)&&ch!='-')ch=getchar();
    if(ch=='-')f=-1,ch=getchar();
    while(isdigit(ch))x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*f;
}
void write(int x)
{
    int f=0;char ch[20];
    if(x==0){putchar('0');putchar('\n');return ;}
    if(x<0){putchar('-'),x=-x;}
    while(x)ch[++f]=x%10+'0',x/=10;
    while(f)putchar(ch[f--]);putchar('\n');
}
int n,m,no,fir[maxn],nxt[maxn],v[maxn],cnte,dfn[maxn],low[maxn],ans[maxn],ins[maxn],stk[maxn],tp,tim,col[maxn],num;
void ade(int u1,int v1){v[cnte]=v1,nxt[cnte]=fir[u1],fir[u1]=cnte++;}
void tar(int u)
{
    dfn[u]=low[u]=++tim,ins[u]=1,stk[++tp]=u;
    view(u,k)
    {
        if(!dfn[v[k]])tar(v[k]),low[u]=min(low[u],low[v[k]]);
        else if(ins[v[k]])low[u]=min(low[u],dfn[v[k]]);
    }
    if(dfn[u]==low[u])
    {
        num++;int cnt=0;
        while(1)
        {
            cnt++;
            col[stk[tp]]=num,ins[stk[tp]]=0;
            if(stk[tp--]==u)break;
        }
        if(cnt==n)no=1;
    }
}
int gx(int x,int f){return x+f*n;}
int main()
{
    int t=read();
    while(t--)
    {
        n=read(),m=read(),no=0;int li=n<<1;
        rep(i,1,li)dfn[i]=0,fir[i]=-1;cnte=tim=num=0;
        rep(i,1,m)
        {
            int x=read(),y=read();
            if(x==y)continue; 
            ade(gx(x,0),gx(y,0)),ade(gx(y,1),gx(x,1));
        }
        int c[2],co=1;c[0]=c[1]=0;
        rep(i,1,li)if(!dfn[i])tar(i);
        if(no){puts("No");continue;}
        rep(i,1,n)if(col[i]==1)co=0;
        rep(i,1,n)
        {
            if(col[gx(i,0)]==col[gx(i,1)]){puts("No");no=1;break;}
            if(col[gx(i,co)]==1)ans[i]=co,c[co]++;
            else ans[i]=co^1,c[co^1]++;
        }
        if(no)continue;
        puts("Yes");
        printf("%d %d\n",c[0],c[1]);
        rep(i,1,n)if(!ans[i])printf("%d ",i);puts("");
        rep(i,1,n)if(ans[i])printf("%d ",i);puts("");
    }
    return 0;
}

Some thoughts

Great ysf mouth Hu! ! !

Guess you like

Origin www.cnblogs.com/xzyf/p/11713839.html