Codeforces Round #345 (Div. 2)

Source: Internet
Author: User

D: Slide the window directly, enumerate the turning point, and slide the end point. Water problem, water problem ....

#include <bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))#definePII pair<int,int>using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intinf=1e9+Ten;intn,a,b,t;CharS[MAXN],T[MAXN];intLOOK[MAXN];intSolveintdir) {    if(dir) Reverse (s+2, s+n+1); REP (i,1, N) look[i]=s[i]=='W'? B +1:1; intans=0; intcost=look[1],cnt=1; intx=1, y=n+1; ///find the last Y     while(y>1) {y--; if(y==1) {y++; Break; } Cost+=a+Look[y]; CNT++; if(cost>T) {CNT--, cost-=a+look[y],y++;  Break; }    }    //cout<< "cnt=" <<cnt<< "cost=" <<cost<< "t=" <<T<<endl;    if(cost<=t) ans=Max (ans,cnt); ///Solve     for(x=1;;) {        if(x+1==y) Break; X++; Cost+=2*a+Look[x]; CNT++;  while(y<=n&&cost>T) { Cost-=look[y]+A; CNT--; Y++; }        if(cost<=t) ans=Max (ans,cnt); }    returnans;}intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); #endif //Online_judge     while(cin>>n>>a>>b>>T) {scanf ("%s", s+1); intans=0; REP (i,0,1) ans=Max (Ans,solve (i)); cout<<ans<<Endl; }    return 0;}/*2 3 10wwhw5 2 4 13hhwhh5 2 4 1000hhwhh3 1 10WHW*/
View Code

Question e: greedy + and look up the set.

Take all the numbers out of the order from small to large, in order to fill in, the first must be filled 1, and then use if the current to fill the number of X, to see the X row and the column, to fill the x must be greater than or equal to the maximum number of rows and columns, if greater than the good processing, take the maximum value of +1 can be, the key is equal to the time. Here use and check set maintenance, will be in the same row or the same column of the number of equal and check set to connect, then each time as long as the need to update and look up the set of ancestors can be, anyway, whichever will find its ancestors. And if a number is not equal in the same column as the peers, then its ancestors are itself. Now the problem is solved, each add a number, check the same column is not equal, if there is an update and check the set of ancestors or it, and to connect it with the ancestors, if not directly updated on the line. In four different cases the code will be more clear.

#include <bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))#definePII pair<int,int>using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intinf=1e9+Ten;intn,m;intA[MAXN]; PII B[MAXN];intN;intFA[MAXN];intVAL[MAXN];intRID[MAXN],CID[MAXN];///The position of the maximum row value, the position of the column maximumintFindintx) {    returnfa[x]==x?x:fa[x]=find (Fa[x]);}intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); #endif //Online_judge     while(cin>>n>>L) {REP (i,1, N) REP (J,1, m) scanf ("%d", &a[(i-1) *m+J]); REP (i,1, N) fa[i]=i; N=n*m; REP (i,1, N) b[i]={a[i],i}; Sort (b+1, b+n+1); REP (i,1, N) fa[i]=i;        MS0 (Val); MS0 (RID);        MS0 (CID); intp1=b[1].second; intR1= (p1-1)/m+1, c1=p1%m; if(c1==0) c1=m; VAL[P1]=1; RID[R1]=cid[c1]=P1; REP (i,2, N) {            intp=B[i].second; intR= (P-1)/m+1, c=p%m; if(c==0) c=m; intX=find (Rid[r]), y=find (Cid[c]); if(a[p]>a[x]&&a[p]>A[y]) {Rid[r]=cid[c]=p; VAL[P]=max (Val[x],val[y]) +1; }            Else if(a[p]>a[x]&&a[p]==A[y]) {Rid[r]=p; VAL[P]=max (val[x]+1, Val[y]); Fa[y]=p; }            Else if(a[p]==a[x]&&a[p]>A[y]) {Cid[c]=p; VAL[P]=max (val[x],val[y]+1); FA[X]=p; }            Else{Rid[r]=cid[c]=p; VAL[P]=Max (Val[x],val[y]); FA[X]=p;fa[y]=p; }} REP (I,1, N) {REP (J,1, M) {                intX=find ((I-1) *m+j); printf ("%d", val[x]); } puts (""); }    }    return 0;}/** * * 3050-3050-7090-701 8-1 2 5 4*/
View Code

Codeforces Round #345 (Div. 2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.