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",&l);
12         getchar();
13         scanf("%s",s+1);
14         sum[0][0]=sum[0][1]=0;
15         for(int i=1;i<=l;i++){
16             sum[i][0]=sum[i-1][0];
17             sum[i][1]=sum[i-1][1];
18             sum[i][s[i]-‘0‘]++;
19         }
20         dp[0]=0;
21         for(int i=1;i<=l;i++){
22             for(int j=1;j<=i;j++){
23                 sum0=sum[i][0]-sum[j-1][0];
24                 sum1=sum[i][1]-sum[j-1][1];
25                 if(sum1<=sum0)continue;
26                 dp[i]=max(dp[i],dp[j-1]+i-j+1);
27             }
28         }
29         printf("%d\n",dp[l]);
30     }
31     return 0;
32 }

SPOJ ROCK

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

SPOJ ROCK的相关文章

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 sma

暑期测试训练3

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

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 705 Distinct Substrings(后缀数组)

[题目链接] http://www.spoj.com/problems/SUBST1/ [题目大意] 给出一个串,求出不相同的子串的个数. [题解] 对原串做一遍后缀数组,按照后缀的名次进行遍历, 每个后缀对答案的贡献为n-sa[i]+1-h[i], 因为排名相邻的后缀一定是公共前缀最长的, 那么就可以有效地通过LCP去除重复计算的子串. [代码] #include <cstdio> #include <cstring> #include <algorithm> usi

SPOJ 3273

传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 1 //SPOJ 3273 2 //by Cydiater 3 //2016.8.31 4 #include <iostream> 5 #include <cstring> 6 #include <ctime> 7 #include <cmath> 8 #include <cstdlib> 9 #include <string> 10 #include

SPOJ CRAN02 - Roommate Agreement

题目链接:http://www.spoj.com/problems/CRAN02/ 题目大意:N个数字组成的序列,和为0的连续子序列的个数.N<1e6 解题思路:计算前缀和,统计每个数字出现的次数,那么对于数字sum[i], 如果存在k个sum[i],则代表有C(k, 2)个序列和为0,而如果sum[i] = 0,则还要累加上对应的k值. 代码: 1 ll n; 2 int a[maxn]; 3 ll sum[maxn]; 4 map<int, int> mmp; 5 6 void so

Hard Rock

Ilya is a frontman of the most famous rock band on Earth. Band decided to make the most awesome music video ever for their new single. In that music video Ilya will go through Manhattan standing on the top of a huge truck and playing amazing guitar s

spoj GCJ1C09C Bribe the Prisoners

题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Cells number i and i+1 are adjacent, and prisoners in adjacent cells are called "neighbours." A wall wi

SPOJ QTREE Query on a tree ——树链剖分 线段树

[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 20005 int T,n,fr[maxn],h[maxn],to[maxn],ne[maxn]