Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0.
The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.
The first line of input contains two integers n and s (1 ≤ n ≤ 100, 1 ≤ s ≤ 1000) — the number of passengers and the number of the top floor respectively.
The next n lines each contain two space-separated integers fi and ti (1 ≤ fi ≤ s, 1 ≤ ti ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i.
Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0.
3 7
2 1
3 8
5 2
11
5 10
2 77
3 33
8 21
9 12
10 64
79
In the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:
1. Move to floor 5: takes 2 seconds.
2. Pick up passenger 3.
3. Move to floor 3: takes 2 seconds.
4. Wait for passenger 2 to arrive: takes 4 seconds.
5. Pick up passenger 2.
6. Go to floor 2: takes 1 second.
7. Pick up passenger 1.
8. Go to floor 0: takes 2 seconds.
This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 int n,s,x,y; 7 scanf("%d %d",&n,&s); 8 int i,ans=s; 9 for(i=1;i<=n;i++) 10 { 11 scanf("%d %d",&x,&y); 12 if(x+y>s) 13 s=x+y; 14 } 15 printf("%d ",s); 16 }
Genos needs your help. He was asked to solve the following programming problem by Saitama:
The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance between string "0011" and string "0110" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.
Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|.
The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000).
The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000).
Both strings are guaranteed to consist of characters '0' and '1' only.
Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.
01
00111
3
0011
0110
2
For the first sample case, there are four contiguous substrings of b of length |a|: "00", "01", "11", and "11". The distance between "01" and "00" is |0 - 0| + |1 - 0| = 1. The distance between "01" and "01" is |0 - 0| + |1 - 1| = 0. The distance between "01" and "11" is|0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string "11". The sum of these edit distances is1 + 0 + 1 + 1 = 3.
The second sample case is described in the statement.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 char a[200005],b[200005]; 5 int s[200005],s2[200005]; 6 int main() 7 { 8 int i,j; 9 int la,lb; 10 scanf("%s %s",a,b); 11 la=strlen(a),lb=strlen(b); 12 if(a[0]=='1') 13 s[0]=1; 14 else 15 s[0]=0; 16 for(i=1;i<la;i++) 17 { 18 if(a[i]=='1') 19 s[i]=s[i-1]+1; 20 else 21 s[i]=s[i-1]; 22 } 23 24 if(a[la-1]=='1') 25 s2[la-1]=1; 26 else 27 s2[la-1]=0; 28 for(i=la-2;i>=0;i--) 29 { 30 if(a[i]=='1') 31 { 32 s2[i]=s2[i+1]+1; 33 } 34 else 35 s2[i]=s2[i+1]; 36 } 37 38 long long ans=0; 39 for(i=0;i<lb;i++) 40 { 41 int ms,ns; 42 if(i<=la-1 && i<=lb-la) 43 ms=s[i],ns=i+1; 44 else if(i>=la-1 && i>=lb-la) 45 ms=s2[la+i-lb],ns=lb-i; 46 else 47 { 48 ns=min(lb-la+1,la); 49 if(i<la) 50 ms=s[i]-s[i-ns]; 51 else 52 ms=s[la-1]; 53 } 54 55 if(b[i]=='0') 56 ans=ans+ms; 57 else 58 ans=ans+(ns-ms); 59 } 60 61 printf("%I64d ",ans); 62 }
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.
Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.
4
1 9
3 1
6 1
7 4
1
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
3
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position1337 with power level 42.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 struct Node 5 { 6 int a; 7 int b; 8 }bee[100005]; 9 10 bool cmp(Node x,Node y) 11 { 12 return x.a<y.a; 13 } 14 15 int c[1000005],s[1000005]; 16 int dp[100005]; 17 18 int main() 19 { 20 int n; 21 int i,j,k; 22 int ans; 23 scanf("%d",&n); 24 memset(c,0,sizeof(c)); 25 for(i=1;i<=n;i++) 26 { 27 scanf("%d %d",&bee[i].a,&bee[i].b); 28 c[bee[i].a]++; 29 } 30 sort(bee+1,bee+n+1,cmp); 31 32 s[0]=c[0]; 33 for(i=1;i<=1000000;i++) 34 { 35 s[i]=s[i-1]+c[i]; 36 } 37 38 dp[0]=0,dp[1]=0; 39 ans=n-1; 40 int len,destroy; 41 for(i=2;i<=n;i++) 42 { 43 len=bee[i].a-bee[i].b; 44 if(len<=0) 45 destroy=i-1; 46 else 47 destroy=s[bee[i].a]-s[len-1]-1; 48 dp[i]=destroy+dp[i-1-destroy]; 49 ans=min(ans,dp[i]+n-i); 50 } 51 52 printf("%d ",ans); 53 54 }
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.
The first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.
The second line contains n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.
Print a single integer — the minimum number of seconds needed to destroy the entire line.
3
1 2 1
1
3
1 2 3
3
7
1 4 4 2 3 2 1
2
In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.
DP题 从小到大枚举串长度 状态转移的方式为:
![](http://codeforces.com/predownloaded/d2/66/d26630b29782e070f335ec9d30c6af454c57a2cb.png)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int a[505],dp[505][505]; 5 int len; 6 int main() 7 { 8 int n; 9 int i,j,k; 10 scanf("%d",&n); 11 for(i=1;i<=n;i++) 12 { 13 scanf("%d",&a[i]); 14 } 15 memset(dp,0,sizeof(dp)); 16 17 for(len=1;len<=n;len++) 18 { 19 for(i=1,j=i+len-1;j<=n;i++,j++) 20 { 21 if(len==1) 22 dp[i][j]=1; 23 else 24 { 25 dp[i][j]=1+dp[i+1][j]; 26 if(a[i]==a[i+1]) 27 dp[i][j]=min(dp[i+2][j]+1,dp[i][j]); 28 for(k=2;i+k<=j;k++) 29 { 30 if(a[i]==a[i+k]) 31 { 32 dp[i][j]=min(dp[i+1][i+k-1]+dp[i+k+1][j],dp[i][j]); 33 } 34 } 35 } 36 } 37 } 38 printf("%d ",dp[1][n]); 39 }
In the spirit of the holidays, Saitama has given Genos two grid paths of length n (a weird gift even by Saitama's standards). A grid path is an ordered sequence of neighbouring squares in an infinite grid. Two squares are neighbouring if they share a side.
One example of a grid path is (0, 0) → (0, 1) → (0, 2) → (1, 2) → (1, 1) → (0, 1) → ( - 1, 1). Note that squares in this sequence might be repeated, i.e. path has self intersections.
Movement within a grid path is restricted to adjacent squares within the sequence. That is, from the i-th square, one can only move to the (i - 1)-th or (i + 1)-th squares of this path. Note that there is only a single valid move from the first and last squares of a grid path. Also note, that even if there is some j-th square of the path that coincides with the i-th square, only moves to (i - 1)-th and (i + 1)-th squares are available. For example, from the second square in the above sequence, one can only move to either the first or third squares.
To ensure that movement is not ambiguous, the two grid paths will not have an alternating sequence of three squares. For example, a contiguous subsequence (0, 0) → (0, 1) → (0, 0) cannot occur in a valid grid path.
One marble is placed on the first square of each grid path. Genos wants to get both marbles to the last square of each grid path. However, there is a catch. Whenever he moves one marble, the other marble will copy its movement if possible. For instance, if one marble moves east, then the other marble will try and move east as well. By try, we mean if moving east is a valid move, then the marble will move east.
Moving north increases the second coordinate by 1, while moving south decreases it by 1. Similarly, moving east increases first coordinate by 1, while moving west decreases it.
Given these two valid grid paths, Genos wants to know if it is possible to move both marbles to the ends of their respective paths. That is, if it is possible to move the marbles such that both marbles rest on the last square of their respective paths.
The first line of the input contains a single integer n (2 ≤ n ≤ 1 000 000) — the length of the paths.
The second line of the input contains a string consisting of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') — the first grid path. The characters can be thought of as the sequence of moves needed to traverse the grid path. For example, the example path in the problem statement can be expressed by the string "NNESWW".
The third line of the input contains a string of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') — the second grid path.
Print "YES" (without quotes) if it is possible for both marbles to be at the end position at the same time. Print "NO" (without quotes) otherwise. In both cases, the answer is case-insensitive.
7
NNESWW
SWSWSW
YES
3
NN
SS
NO
In the first sample, the first grid path is the one described in the statement. Moreover, the following sequence of moves will get both marbles to the end: NNESWWSWSW.
In the second sample, no sequence of moves can get both marbles to the end.