ZOJ 2301 discretization

Source: Internet
Author: User
Tags acos cmath

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&LT;B.R;} BOOL Inter (node I,node j) {return J.R&GT;=I.L&AMP;&AMP;J.L&LT;=I.R;} void Update (node &tmp,node &j) {if (TMP.L&GT;J.L&AMP;&AMP;TMP.R&LT;J.R) {ne[++cnt]= (node) {tmp.r+           1,J.R};           J= (node) {J.L,TMP.L-1};       Return   } if (TMP.L&LT;=J.L&AMP;&AMP;TMP.R&GT;=J.R) {J.l=-1;j.r=-1;return;}   if (TMP.L&GT;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&GT;=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&LT;B.R;} BOOL Inter (node I,node j) {return J.R&GT;=I.L&AMP;&AMP;J.L&LT;=I.R;}           void Update (node &tmp,node &j) {if (TMP.L&GT;J.L&AMP;&AMP;TMP.R&LT;J.R) {j= (node) {J.L,TMP.L-1};           Ne[++cnt]= (node) {TMP.R+1,TMP.R};       Return   } if (TMP.L&LT;=J.L&AMP;&AMP;TMP.R&GT;=J.R) {J.l=-1;j.r=-1;return;}   if (TMP.L&GT;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

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.