Milking Time
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0…N-1) so that she produces as much milk as possible.
Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houri ≤ N), an ending hour (starting_houri < ending_houri ≤ N), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.
Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ R ≤ N) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.
Input
- Line 1: Three space-separated integers: N, M, and R
- Lines 2…M+1: Line i+1 describes FJ’s ith milking interval withthree space-separated integers: starting_houri , ending_houri , and efficiencyi
Output
- Line 1: The maximum number of gallons of milk that Bessie can product in the N hours
Sample Input
12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
Sample Output
43
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long inf= 0x3f3f3f3f3f3f;
const int N=1e3+5;
const int mod=998244353;
struct node{
int a,b,v;
}st[N];
bool cmp(node a,node b){
return a.a<b.a;
}
int dp[N];
int main(){
std::ios::sync_with_stdio(false);
int n,m,r,ans=0,i;
cin>>n>>m>>r;
for(i=0;i<m;i++){
cin>>st[i].a>>st[i].b>>st[i].v;
st[i].b+=r;
}
sort(st,st+m,cmp);
for(i=0;i<m;i++){
dp[i]=st[i].v;
for(int j=0;j<i;j++)
if(st[j].b<=st[i].a)
dp[i]=max(dp[i],dp[j]+st[i].v);
ans=max(ans,dp[i]);
}
cout<<ans<<endl;
return 0;
}
数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:
有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
已经告诉你了,这是个DP的题目,你能AC吗?
Input
输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内。
Output
对于每个测试实例,输出可能得到的最大和,每个实例的输出占一行。
Sample Input
1
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long inf= 0x3f3f3f3f3f3f;
const int N=2e5+10;
const int mod=998244353;
int main(){
std::ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
int n,i;
cin>>n;
int a[105][105],dp[105][105];
for(i=1;i<=n;i++){
for(int j=1;j<=i;j++)
cin>>a[i][j];
}
for(i=1;i<=n;i++){
dp[n][i]=a[n][i];
}
for(i=n-1;i>0;i--){
for(int j=1;j<=i;j++){
dp[i][j]=a[i][j]+max(dp[i+1][j],dp[i+1][j+1]);
}
}
cout<<dp[1][1]<<endl;
}
return 0;
}
Phalanx
Today is army day, but the servicemen are busy with the phalanx for the celebration of the 60th anniversary of the PRC.
A phalanx is a matrix of size nn, each element is a character (a~z or A~Z), standing for the military branch of the servicemen on that position.
For some special requirement it has to find out the size of the max symmetrical sub-array. And with no doubt, the Central Military Committee gave this task to ALPCs.
A symmetrical matrix is such a matrix that it is symmetrical by the “left-down to right-up” line. The element on the corresponding place should be the same. For example, here is a 33 symmetrical matrix:
cbx
cpb
zcc
Input
There are several test cases in the input file. Each case starts with an integer n (0<n<=1000), followed by n lines which has n character. There won’t be any blank spaces between characters or the end of line. The input file is ended with a 0.
Output
Each test case output one line, the size of the maximum symmetrical sub- matrix.
Sample Input
3
abx
cyb
zca
4
zaba
cbab
abbc
cacq
0
Sample Output
3
3
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long inf= 0x3f3f3f3f3f3f;
const int N=1e3+5;
const int mod=998244353;
char s[N][N];
int dp[N][N];
int main(){
//std::ios::sync_with_stdio(false);
int n;
while(cin>>n&&n){
int i,j,ans=1;
for(i=1;i<=n;i++){
scanf("%s",s[i]+1);
}
for(i=1;i<=n;i++){
for(j=n;j>0;j--){
dp[i][j]=1;
if(i==1||j==n) continue;
int k=dp[i-1][j+1];
for(int z=1;z<=k;z++){
if(s[i-z][j]==s[i][j+z])
dp[i][j]++;
else
break;
}
ans=max(ans,dp[i][j]);
}
}
cout<<ans<<endl;
}
return 0;
}