Topic Link: Test instructions is said, there are a number of balls starting from 1 to increment sequentially, they are all black, now give n operations (AI,BI,CI), each operation is the number AI into the bi range of all the balls painted CI color (black or white), and then after N times given After the operation, the left and right end points of the longest continuous white interval are obtained.
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm > #include <set>using namespace std;typedef long long ll;typedef unsigned long long Ull; #define MM (A, B) memset (A, B , sizeof (a)); const double EPS = 1e-10;const int inf = 0x3f3f3f3f;const double Pi=acos ( -1); const int Maxn=100000;int cnt;str UCT node{ll L,r;} Ne[2*maxn+10];bool CMP (node A,node b) {if (A.L!=B.L) return a.l<b.l; else return A.R<B.R;} BOOL Inter (node I,node j) {return J.R>=I.L&&J.L<=I.R;} void Update (node &tmp,node &j) {if (TMP.L>J.L&&TMP.R<J.R) {ne[++cnt]= (node) {tmp.r+ 1,J.R}; J= (node) {J.L,TMP.L-1}; Return } if (TMP.L<=J.L&&TMP.R>=J.R) {J.l=-1;j.r=-1;return;} if (TMP.L>J.L) j.r=tmp.l-1; else j.l=tmp.r+1;} int main () {int n; while (~SCANF ("%d", &n)) {int l,r; Char op[5]; cnt=0; for (int i=1;i<=n;i++) {scanf ("%d%d", &l,&r); if (l>r) swap (L,R);//The title only says the given range scanf ("%s", op); if (op[0]== ' W ') {ne[++cnt].l=l;ne[cnt].r=r;} else {node tmp= (node) {l,r}; int curcnt=cnt; for (int j=1;j<=curcnt;j++) if (Inter (Tmp,ne[j])) update (tmp,ne[j]); }} sort (ne+1,ne+cnt+1,cmp); Node ans= (node) {1,0}; for (int i=1;i<=cnt;) {node cur= (node) {NE[I].L,NE[I].R}; if (cur.l<0) {i++;continue;} while (CUR.R+1>=NE[I].L) {Cur.r=max (NE[I].R,CUR.R); i++;//note the newly added interval is not//must be greater than the current if (i>cnt) BreaK } if (cur.r-cur.l+1>ans.r-ans.l+1) ans=cur; } if (ans.r!=0) printf ("%lld%lld\n", ANS.L,ANS.R); else printf ("Oh, My god\n"); } return 0;}
Analysis: The maximum number of int range of the ball, stating that the direct open array is certainly not possible, consider discretization, after all, n<=2000, each
Nodes represent an interval, and each time a new Black node appears, the interference with the existing white bands is considered, and finally
It is good to record the longest length of all the white bands, refer to the article;
WA Code:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm > #include <set>using namespace std;typedef long long ll;typedef unsigned long long Ull; #define MM (A, B) memset (A, B , sizeof (a)); const double EPS = 1e-10;const int inf = 0x3f3f3f3f;const double Pi=acos ( -1); const int Maxn=100000;int cnt;str UCT node{ll L,r;} Ne[2*maxn+10];bool CMP (node A,node b) {if (A.L!=B.L) return a.l<b.l; else return A.R<B.R;} BOOL Inter (node I,node j) {return J.R>=I.L&&J.L<=I.R;} void Update (node &tmp,node &j) {if (TMP.L>J.L&&TMP.R<J.R) {j= (node) {J.L,TMP.L-1}; Ne[++cnt]= (node) {TMP.R+1,TMP.R}; Return } if (TMP.L<=J.L&&TMP.R>=J.R) {J.l=-1;j.r=-1;return;} if (TMP.L>J.L) j.r=tmp.l-1; else j.l=tmp.r+1;} int main () {int N while (~SCANF ("%d", &n)) {int l,r; Char op[5]; cnt=0; for (int i=1;i<=n;i++) {scanf ("%d%d", &l,&r); scanf ("%s", op); if (op[0]== ' W ') {ne[++cnt].l=l;ne[cnt].r=r;} else {node tmp= (node) {l,r}; for (int j=1;j<=cnt;j++) if (Inter (Tmp,ne[j])) update (tmp,ne[j]); }} sort (ne+1,ne+cnt+1,cmp); Node ans= (node) {0,0}; for (int i=1;i<=cnt;) {node cur= (node) {NE[I].L,NE[I].R}; if (cur.l<0) continue; while (Inter (Cur,ne[i])) {cur.r=ne[i].r;i++;} if (cur.r-cur.l+1>ans.r-ans.l+1) ans=cur; } if (ans.l!=0&&ans.r!=0) printf ("%d%d\n", ANS.L,ANS.R); else printf ("Oh, My god\n"); } return 0;}
ZOJ 2301 discretization