Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1420 Accepted Submission(s): 415
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn = 100100; 7 struct tnode 8 { 9 int s,e; 10 int ms,me; 11 int keyinterval; 12 int id; 13 }c[maxn]; 14 15 bool operator < (const tnode &a , const tnode &b) 16 { 17 return (a.ms <b.ms ||(a.ms==b.ms&&a.me<b.me)); 18 } 19 int n , i; 20 void init() 21 { 22 for(i=0 ; i<n ;i++ ){ 23 scanf("%d%d",&c[i].s,&c[i].e); 24 c[i].id=i; 25 c[i].keyinterval = (c[i].e - c[i].s +2)/2 ; 26 c[i].ms=c[i].s+(c[i].e - c[i].s -1)/2; 27 c[i].me=c[i].s+1; 28 if((c[i].e-c[i].s)%2==0) 29 ++c[i].me; 30 } 31 sort(c,c+n); 32 } 33 bool work() 34 { 35 int now_s , now_e ,last_e; 36 last_e=0; 37 for(int i=0 ; i<n ;i++) 38 { 39 now_s =c[i].s; 40 if(now_s<last_e) now_s=last_e; 41 now_e=now_s+c[i].keyinterval; 42 if(now_e>c[i].e) return false; 43 last_e = now_e; 44 } 45 return true ; 46 } 47 48 int main() 49 { 50 while(scanf("%d",&n),n!=0) 51 { 52 init(); 53 if(work()) 54 printf("YES\n"); 55 else 56 printf("NO\n"); 57 } 58 return 0; 59 }
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<algorithm> 6 using namespace std; 7 const int maxn = 100100; 8 struct tnode 9 { 10 int s,e; 11 int ms,me; 12 int keyinterval; 13 int id; 14 bool operator < (const tnode &b) const 15 { 16 return (ms <b.ms ||(ms==b.ms&&me<b.me)); 17 } 18 }; 19 20 int n , i; 21 tnode cc; 22 vector<tnode>c; 23 void init() 24 { 25 c.clear(); 26 for(i=0 ; i<n ;i++ ){ 27 scanf("%d%d",&cc.s,&cc.e); 28 cc.id=i; 29 cc.keyinterval = (cc.e - cc.s +2)/2 ; 30 cc.ms=cc.s+(cc.e - cc.s -1)/2; 31 cc.me=cc.s+1; 32 if((cc.e-cc.s)%2==0) 33 ++cc.me; 34 c.push_back(cc); 35 } 36 sort(c.begin(),c.end()); 37 } 38 bool work() 39 { 40 int now_s , now_e ,last_e; 41 last_e=0; 42 for(int i=0 ; i<n ;i++) 43 { 44 now_s =c[i].s; 45 if(now_s<last_e) now_s=last_e; 46 now_e=now_s+c[i].keyinterval; 47 if(now_e>c[i].e) return false; 48 last_e = now_e; 49 } 50 return true ; 51 } 52 53 int main() 54 { 55 while(scanf("%d",&n),n!=0) 56 { 57 init(); 58 if(work()) 59 printf("YES\n"); 60 else 61 printf("NO\n"); 62 } 63 return 0; 64 }
然后自己有写了一次..........!
代码:
手动的扩栈....
#program comment (linker ,"/STACK :102400000 102400000")
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<iostream> 5 #include<algorithm> 6 #pragma comment(linker, "/STACK:102400000,102400000") //由于用内分装之后会出现溢栈的情况,所以手动扩栈 7 using namespace std; 8 9 const int maxn =100100 ; 10 11 struct tnode 12 { 13 int s,e; 14 int ms,me; 15 int mid; 16 bool operator < (const tnode b) const 17 { 18 return (ms<b.ms||(ms==b.ms)&&me<b.me); 19 } 20 }; 21 class node 22 { 23 private: 24 tnode str[maxn]; 25 int i; 26 public : 27 int n; 28 void init(); 29 bool work(); 30 }; 31 32 void node::init() 33 { 34 for(i=0;i<n;i++) 35 { 36 scanf("%d%d",&str[i].s,&str[i].e) ; 37 str[i].mid=(str[i].e-str[i].s)/2 +1 ; 38 str[i].ms= str[i].s+(str[i].e-str[i].s-1)/2 ; 39 str[i].me=str[i].s+1 ; 40 if((str[i].e-str[i].s)%2==0) str[i].me++; 41 } 42 sort(str,str+n); 43 } 44 bool node::work() 45 { 46 int temp_s,temp_e,last_e=0; 47 for( i=0 ; i<n ; i++ ) 48 { 49 temp_s=str[i].s; 50 if(temp_s<last_e) temp_s=last_e; 51 temp_e = temp_s+str[i].mid; 52 if(temp_e>str[i].e) return false; 53 last_e=temp_e; 54 } 55 return true ; 56 } 57 int main() 58 { 59 node a; 60 while(scanf("%d",&a.n)!=EOF&&a.n) 61 { 62 a.init(); 63 if(a.work())printf("YES\n"); 64 else printf("NO\n"); 65 } 66 return 0; 67 }