0x03 prefix and differential

Prefix and

[Example] BZOJ1218 laser bombs

And calculating two prefix, reuse-repellent capacity of the original processing can be calculated answer.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 const int maxn=5000+10;
 7 int n, r, sum[maxn][maxn];
 8 
 9 int main() {
10     scanf("%d%d", &n, &r);
11     int x, y, v, maxx=0, maxy=0;
12     maxx=maxy=r;
13     for (int i=1; i<=n; ++i) {
14         scanf("%d%d%d", &x, &y, &v);
15         ++x; ++y;
16         maxx=max(maxx, x); maxy=max(maxy, y);
17         sum[x][y]=v;
18     }
19     for (int i=1; i<=maxx; ++i) {
20         for (int j=1; j<=maxy; ++j) {
21             sum[i][j]+=sum[i-1] [J] + SUM [I] [J- . 1 ] -sum [I- . 1 ] [J- . 1 ];
 22 is          }
 23 is      }
 24      // Enumeration r a square area of 
25      int ANS = 0 ;
 26 is      for ( int I = R & lt; I <= Maxx; ++ I) {
 27          for ( int J = R & lt; J <= Maxy; ++ J) {
 28              ANS = max (ANS, SUM [I] [J] -sum [ IR] [J] -sum [I] [JR] + SUM [IR] [JR ]);
 29          }
 30      } 
 31 is      the printf ( " % D \ n- " , ANS);
 32     return 0;
33 }

The difference

[Example] CH0304 IncDec Sequence

Obtaining a sequence of difference sequence b, so that B n-+. 1 = 0, the target will be B 2 , B . 3 ... B n- becomes all-0.

The sequence of a segment [l, r] plus d, which changes the difference sequence is B L plus d, B R & lt +. 1 Save d.

From B . 1 , B 2 ... B n- optionally two numbers methods can be divided into four categories:

1. Select B I and B J

2. Select B . 1 and B J

3.选B I sum B N Tasu 1

4. Option B . 1 and B n-+. 1 does not make sense, this method is selected from

Provided the sum of an integer p, negative sum of q, so as to first try to match the number of positive and negative selection operation 1 executable min (p, q) times.

The remaining | PQ | unpaired, 2 and operation 3 to perform operations, totaling | PQ | times.

Therefore, a minimum number of operations min (p, q) + | pq | = max (p, q) times, where the operation according to a selection of 2 and 3, to produce | pq | +1 seed B . 1 value, i.e., a possible sequence there | pq | +1 species.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 using namespace std;
 7 const int maxn=100000+10;
 8 long long n, a[maxn], s[maxn], p=0, q=0;
 9 
10 int main() {
11     scanf("%lld", &n);
12     for (int i=1; i<=n; ++i)
13         scanf("%lld", &a[i]);
14     s[1]=a[1]; s[n+1]=0;
15     for (int i=2; i<=n; ++i) {
16         s[i]=a[i]-a[i-1];
17     }
18     for (int i=2; i<=n; ++i) {
19         if (s[i]>0) p+=s[i];
20         else if (s[i]<0) q-=s[i];
21     }
22     printf("%lld\n%lld\n", max(p,q), abs(p-q)+1);
23     return 0;
24 }

[Example] POJ3263 Tallest Cow

If a specified relationship A I and B I can see each other, they will array A I + 1'd to B I minus the number of 1-1, resulted in a relative height relationships of all cows with the highest p cattle.

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <map>
 7 using namespace std;
 8 const int maxn=100000+10;
 9 int n, p, h, m, c[maxn], d[maxn];
10 map<pair<int, int>, bool> ext;
11 
12 int main() {
13     scanf("%d%d%d%d", &n, &p, &h, &m);
14     int a, b;
15     for (int i=1; i<=m; ++i) {
16         scanf("%d%d", &a, &b);
17         if (a>b) swap(a,b);
18         if (ext[make_pair(a,b)]) continue;
19         d[a+1]--, d[b]++;
20         ext[make_pair(a,b)]=true;
21     }
22     for (int i=1; i<=n; ++i) {
23         c[i]=c[i-1]+d[i];
24         printf("%d\n", h+c[i]);
25     }
26     return 0;
27 }

 

Guess you like

Origin www.cnblogs.com/kkkstra/p/11104629.html