Sweet and Sour Rock+spoj+简单dp
来源:互联网 发布:java drawimage 厘米 编辑:程序博客网 时间:2024/06/01 18:08
154. Sweet and Sour Rock
Problem code: ROCK
A manufacturer of sweets has started production of a new type of sweet called rock. Rock comes in sticks composed of one-centimetre-long segments, some of which are sweet, and the rest are sour. Before sale, the rock is broken up into smaller pieces by splitting it at the connections of some segments.
Today's children are very particular about what they eat, and they will only buy a piece of rock if it contains more sweet segments than sour ones. Try to determine the total length of rock which can be sold after breaking up the rock in the best possible way.
Input
The input begins with the integer t, the number of test cases. Then t test cases follow.
For each test case, the first line of input contains one integer N - the length of the stick in centimetres (1<=N<=200). The next line is a sequence of N characters '0' or '1', describing the segments of the stick from the left end to the right end ('0' denotes a sour segment, '1' - a sweet one).
Output
For each test case output a line with a single integer: the total length of rock that can be sold after breaking up the rock in the best possible way.
Example
Sample input:215100110001010001160010111101100000Sample output:913
解决方案:假设在j处切,这状态方程为:dp[i]=max{dp[i],dp[j]+i-j+1}前提是i到j之间甜的比酸的多。
code:#include<iostream>#include<cstring>#include<cstdio>#define MMAX 220using namespace std;int sum[2][MMAX];int dp[MMAX];char str[MMAX];int main(){ int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); memset(sum,0,sizeof(sum)); getchar(); scanf("%s",str); for(int i=1;i<=n;i++){ sum[0][i]=sum[0][i-1]; sum[1][i]=sum[1][i-1]; sum[str[i-1]-'0'][i]++; } dp[0]=0; for(int i=1;i<=n;i++){ dp[i]=dp[i-1]; for(int j=1;j<=i;j++){ int m0=sum[0][i]-sum[0][j-1]; int m1=sum[1][i]-sum[1][j-1]; if(m0>=m1) continue; dp[i]=max(dp[i],dp[j-1]+i-j+1); } } printf("%d\n",dp[n]); } return 0;}
- Sweet and Sour Rock+spoj+简单dp
- SPOJ 154 Sweet and Sour Rock
- SPOJ154 Sweet and Sour Rock dp
- sour
- sweet and other meal
- 【SPOJ-TOMJERRY】Tom and Jerry【概率DP】【记忆化搜索】
- Mix and Build(简单DP)
- SWEET
- Sweet!!!
- UVALive 6840Sweet War (博弈dp)
- Game of Paper, Rock and scissors
- SPOJ 1128 数位DP
- SPOJ - PARTPALI(数位dp)
- SPOJ FAVDICE_概率论DP
- SPOJ LUCIFER (数位dp)
- SPOJ KPSUM 数位DP
- SPOJ GCJ1C09C DP
- [SPOJ PUTNIK]区间DP
- lua学习初探【二】
- 面对不断升级的内核,如何学习linux设备驱动
- GoConvey_初步认识
- SPOJ MULTQ3 线段树
- 基数排序
- Sweet and Sour Rock+spoj+简单dp
- ThreadLocal的几种误区
- Python:== 和 is
- JavaFX文档(6)开始JavaFX之旅——4 使用FXML来创建用户界面
- 欧拉回路,欧拉道路
- C++之练习题29
- Netfilter之DNAT和SNAT
- IO流练习---学生成绩排序存储(map中按值排序方法与各集合间的转换)
- 【C++基础 07】运算符重载