Problem E SPOJ ROCK

Description

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.

Sample Input

Sample input:
2
15
100110001010001
16
0010111101100000

Sample output:
9
13

•dp(i)表示将前i段糖进行切割后最多能卖掉的数量

•状态转移方程:

•考虑在第j段之前切一刀

•dp(i)=max{ dp(i) , dp(j-1) +i–j+1}

•( 1 <= j <= i 且在[j,i]区间内甜的比酸的多)

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 205;
 6 char str[maxn];
 7 int dp[maxn],sum[maxn][2];
 8
 9 int main(){
10     int T,len;
11     scanf("%d",&T);
12     while(T--){
13         scanf("%d %s",&len,str+1);
14         sum[0][0] = sum[0][1] = 0;
15         for(int i = 1;i <= len;i++){
16             sum[i][0] = sum[i-1][0];
17             sum[i][1] = sum[i-1][1];
18             sum[i][str[i]-‘0‘]++;//记录‘0’,‘1’出线的次数
19         }
20         dp[0] = 0;
21         for(int i = 1;i <= len;i++){
22             dp[i] = dp[i-1];
23             for(int j = 1;j <= i;j++){
24                 int sum0 = sum[i][0]-sum[j-1][0];
25                 int sum1 = sum[i][1]-sum[j-1][1];
26                 if(sum1 <= sum0)    continue;//‘0‘比‘1’多的时候
27                 dp[i] = max(dp[i],dp[j-1]+i-j+1);//状态转移方程
28             }
29         }
30         printf("%d\n",dp[len]);
31     }
32     return 0;
33 }

Problem E SPOJ ROCK

时间: 2024-10-09 13:13:39

Problem E SPOJ ROCK的相关文章

Problem B SPOJ DCEPC11I

Description Vaibhav Sir and Saikat Sir, after completing their graduation, got a job together at a store. Their boss at the store was Sidharth Sir, who was very strict and did not want anyone to have fun at the job. He gave Vaibhav and Saikat sir a v

Problem A SPOJ SUB_PROB

Description String Matching is an important problem in computer science research and finds applications in Bioinformatics, Data mining,pattern recognition, Internet security and many more areas. The problem we consider here is a smaller version of it

SPOJ ROCK

DP题,这题居然是1星题!!太大打击了 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 #define N 205 5 char s[N]; 6 int sum[N][2],dp[N]; 7 int main(){ 8 int t,l,sum0,sum1; 9 scanf("%d",&t); 10 while(t--){ 11 scanf("%d",

CSU-ACM暑假集训基础组七夕专场

•Problem A Codeforces 20C       最短路(dij,spfa) •题意:给出一张n个点m条边的无向图 (2 ≤ n ≤ 105, 0 ≤ m ≤ 105),输出从1到n的任意一条最短路径. •解法:dijkstra或者spfa,用pre数组记录到达每个点最短距离的前驱结点. •注意:路径的长度范围是 1 ≤ wi ≤ 106,从1到n的最短路径长度可能超出int范围. •没有路径从1到达n时要输出-1 1 #include <cstdio> 2 #include &

SPOJ GNYR09F 数字上的找规律DP

Problem C      SPOJ GNYR09F dp题,dp可能刚刚开始对大家来说比较难,但是静下心来分析还是比较简单的: dp(i ,j ,k)表示前i个数中,当前累积和为j,末尾数字为k的方案数. 考虑第i个位置的2种情况: 1.放0:dp(i,j,0) = dp(i-1,j,0) + dp(i-1,j,1) 2.放1:dp(i,j,1)= dp(i-1,j,0) 因为每一行最开始的状态均从i=j+1,dp(i,j,0)=0,dp(i,j,1)=1开始的,但因为这样子开头均为1,那些

Spoj PRIME1 - Prime Generator

题意翻译 求给定的两个数之间的素数 Translated by @kaiming 题目描述 Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! 输入输出格式 输入格式: The input begins with the number t of test cas

暑假集训-个人赛第四场

ID Origin Title   10 / 52 Problem A SPOJ AMR10A Playground     Problem B SPOJ AMR10B Regex Edit Distance     Problem C SPOJ AMR11C Robbing Gringotts   1 / 14 Problem D SPOJ AMR10D Soccer Teams   0 / 3 Problem E SPOJ AMR10E Stocks Prediction   17 / 19

数论十题

数论十题 Problem Zero:[neerc2011]Gcd guessing game 现在有一个数x,1 ≤ x≤ n,告诉你n,每次你可以猜一个数y,如果x==y则结束,否则返回gcd(x,y),问最少只要几次就可以保证猜出答案. 本题纯属娱乐.仅仅是一个GCD的游戏,跑题了. 因为本题要求最坏情况,我们直观地猜想就是每次返回都是1.由于答案有可能是质数,而判定一个数,必须要把含有这个质因子的数问一遍.于是,我们引出这样一个思路,将所有1-n的质数分组,每组的积<=n,答案就是组数.

暑期测试训练3

1.Codeforces 20C spfa算法的简单题,在这个过程中多了一个记录连接最短路径上的前一个节点的位置的数组,然后将这个数组逆向输出 在这道题目中,我路径数组范围居然忘记*2了,结果一直报错,找了好久,%>_<% #include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std; #define N 100010 #