Now we have to go through a zebra crossing. the zebra crossing consists of N alternate black bars and white bars. The first is black bars. The length of the foot is S. It is required that no part of his foot should touch the black line symbolizing evil. The parts before and after the first line are white. You can choose any position before the first line. However, once the starting position is selected, the length of each step must be K. Check whether it is possible to pass the zebra crossing without hitting the black bar, that is, after the nth line.
This is also a simulated competition question !!!
I already have a lot of questions about my IQ. Why is it just one step away from the positive solution? I can't think about it every time.
Let's talk about my mistake. Misunderstanding: I listed n inequalities and determined whether it could step on a black block. I * k-x> = A [I], I * k-x + S <= A [I + 1]-kuan [I + 1], I is a black block, but a few steps are a problem, it is also a problem whether this white block can be used. So the correct solution should be .......
Positive Solution: Because the white block does not have to go, we only need to judge whether it can go to the Black Block. We first set the start position and end position of each Black Block mod k, and maps it to a range of (0, k-1), if X> Y, corresponds to (x, k-1), (0, Y, determines whether a point in (0, k-1) is not covered.
But pay attention to one problem. If the length of a Black Block is greater than K-S + 1, it cannot be solved because it cannot be crossed.
Many people say that this question is well handled using open intervals. In fact, closed intervals are also well handled.
Conclusion: I often forget that one thing is mod. Every time I is used to control the scope, I is forgotten about the uncertainty of the variable. mod is a good weapon for the uncertainty problem, because it has nothing to do with I, using MoD can greatly reduce the complexity of the program and the complexity of thinking. You can't forget it later
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 long long p[500005]; 6 struct node 7 { 8 int from,to; 9 }a[500005]; 10 int n; 11 int s,k; 12 int T; 13 int m; 14 long long len; 15 void read(int &x){ 16 char ch = getchar(); while (ch < ‘0‘ || ch > ‘9‘) ch = getchar(); 17 for (x = 0; ch >= ‘0‘ && ch <= ‘9‘; ch = getchar()) x = x*10+ch-48; 18 } 19 bool cmp(node u,node v) 20 { 21 if(u.from!=v.from)return u.from<v.from; 22 return u.to>v.to; 23 } 24 bool rt; 25 int l,r; 26 int main() 27 { 28 read(T); 29 while(T--) 30 { 31 len=0; 32 rt=false; 33 read(s); 34 s--; 35 read(k); 36 read(n); 37 for(int i=1;i<=n;i++) 38 { 39 int x; 40 read(x); 41 len+=x; 42 p[i]=len; 43 } 44 m=0; 45 for(int i=1;i<=n;i+=2) 46 { 47 if(p[i]-p[i-1]>=k-s) 48 { 49 // cout<<p[i]-p[i-1]<<endl; 50 puts("NIE"); 51 rt=true; 52 break; 53 } 54 long long x=p[i-1]-s,y=p[i]-1; 55 x=(x%k+k)%k; 56 y=y%k; 57 // cout<<x<<" "<<y<<endl; 58 if(x<=y){ 59 a[++m].from=x; 60 a[m].to=y; 61 } 62 else 63 { 64 a[++m].from=x; 65 a[m].to=k-1; 66 a[++m].from=0; 67 a[m].to=y; 68 } 69 } 70 if(rt)continue; 71 // cout<<"find"; 72 sort(a+1,a+m+1,cmp); 73 if(a[1].from>0) 74 { 75 puts("TAK"); 76 continue; 77 } 78 l=a[1].from;r=a[1].to; 79 /*for(int i=1;i<=m;i++) 80 printf("%d %d\n",a[i].from,a[i].to);*/ 81 for(int i=2;i<=m;i++) 82 { 83 // cout<<a[i].from<<" "<<r<<endl; 84 if(a[i].from>r+1) 85 { 86 //cout<<i<<r<<endl; 87 rt=true; 88 puts("TAK"); 89 break; 90 } 91 if(a[i].from<=r+1 && a[i].to>=r) 92 { 93 r=a[i].to; 94 } 95 } 96 if(rt)continue; 97 if(r<k-1) 98 { 99 puts("TAK");100 }101 else102 {103 puts("NIE");104 }105 }106 return 0;107 }108 109 View code