codeforces 477D
题意:给定一个长度<=5000的二进制字符串S,以及一个初始为0的n,有一下两种操作:
1. 输出n(n>=1并且为2进制形式输出)
2.n=n+1
每次选择一种操作。。
求:1.有多少方案能构成该字符串 (%(10^9+7))
2.最少需要多少次能构成该字符串(%(10^9+7))
思路:
对于第一问:
能容易想到O(n^3)的dp
f[i][j] += f[k][i-1](其中[k,j-1]构成的数<=[i, j]组成的数,并且i,k位置为'1')
但是这样时限上显然不够,因为比较大小太耗时间了。。
由于是01串,我们可以先预处理same[i][j]表示以i开头和以j开头的数字最多多少个相同的。。这样判断就可以O(1)了
对于第二问:
还是可以用dp。。
f[i][j]表示以j结尾,[i, j]为最后一个数时最少分为几段。。思路跟上面差不多。。
但是比较大小可能需要一点技巧。。
基于最后结尾的数多一位,那么add操作次数*2,那么也就是说当最后的数很大是(add>=|S|),越小的结尾的数越优
所以,对于最后结尾的数比较小的,直接算出来,因为答案不会很大。。
否则的话,直接找结尾的数最小的就是最优解了。
当然,也可以基于上面的结论直接贪心。。
code:
#include <bits/stdc++.h>
#define M 1000000007
#define Inf 0x3fffffff
using namespace std;
char s[];
int same[][], f[][], c[][]; inline void add(int& c, const int &a, const int& b){
c = a + b;
if (c >= M) c -= M;
} int bigger(const int& x, const int& y, const int& k){
if (y <= ) return ;
int len = same[y][x];
if (len >= k) return ;
return s[x+len] >= s[y+len];
} void solve(){
int n = strlen(s + );
for (int i = n; i >= ; --i)
for (int j = i; j <= n; ++j)
if (s[i] == s[j]) same[i][j] = same[i+][j+] + ;
else same[i][j] = ;
for (int i = ; i <= n; ++i)
f[][i] = ;
c[][] = f[][];
for (int i = ; i <= n; ++i){
for (int j = i; j > ; --j) if (s[j] == ''){
add(f[j][i], f[j][i], c[j-][min(i-j, j-)]);
if (bigger(j, j--(i-j), i-j+)){
// if (j == 11 && i == 17) printf("%d %d %d\n", j, j-1-(i-j), i-j+1);
add(f[j][i], f[j][i], f[j--(i-j)][j-]);
}
}
for (int j = i; j >= ; --j)
add(c[i][i-j+], c[i][i-j], f[j][i]);
}
int ans1 = ;
for (int i = ; i <= n; ++i)
add(ans1, ans1, f[i][n]);
for (int i = ; i <= n; ++i)
for (int j = i; j <= n; ++j) f[i][j] = Inf;
for (int i = ; i <= n; ++i)
f[][i] = ;
for (int i = ; i <= n; ++i)
for (int j = ; j <= n; ++j) c[i][j] = Inf;
c[][] = ;
for (int i = ; i <= n; ++i){
for (int j = i; j > ; --j) if (s[j] == ''){
f[j][i] = min(f[j][i], c[j-][min(i-j, j-)] + );
if (bigger(j, j--(i-j), i-j+))
f[j][i] = min(f[j][i], f[j--(i-j)][j-] + );
}
for (int j = i; j >= ; --j)
c[i][i-j+] = min(c[i][i-j], f[j][i]);
}
int ans2 = Inf, x;
for (int i = n; i >= ; --i) if (f[i][n] < Inf){
if (n - i > ) break;
x = ;
for (int j = i; j <= n; ++j)
x = (x << ) + s[j] - '';
ans2 = min(ans2, x+f[i][n]);
}
if (ans2==Inf)
for (int i = n-; i >= ; --i) if (f[i][n] < Inf){
x = ;
for (int j = i; j <= n; ++j){
x = (x << ) + s[j] - '';
if (x >= M) x-=M;
}
ans2 = (x + f[i][n]) % M;
break;
}
printf("%d\n%d\n", ans1, ans2 % M);
} int main(){
while (scanf("%s", s+) != EOF){
solve();
}
}
codeforces 477D的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- iOS.XcodeUsage
1. Customizing Xcode File Templates http://blog.highorderbit.com/2009/03/15/customizing-xcode-cocoa- ...
- 1.1 Java 的概述
[什么是java]:Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是有SunMicrosystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE,Jav ...
- MySQL学习笔记-数据库内存
数据库内存 InnoDB存储引擎内存由以下几个部分组成:缓冲池(buffer pool).重做日志缓冲池(redo log buffer)以及额外的内存池(additional memory pool ...
- UI设计中颜色的前进色与后退色
暖色调的颜色属于前进色.膨胀色可以使物体的视觉效果变大,而收缩色可以使物体的视觉效果变小. 颜色的另外一种效果.有的颜色看起来向上凸出,而有的颜色看起来向下凹陷,其中显得凸出的颜色被称为前进色,而 ...
- MySQL中varchar与char区别
MySQL中varchar与char区别(转) MySQL中varchar最大长度是多少? 一. varchar存储规则: 4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字 ...
- maven web+spring mvc项目没有出现src/main/java路径
直接在main 文件夹下创建java可以解决 https://www.cnblogs.com/zhujiabin/p/6343462.html
- 使用GZipStream压缩和解压文件
最近做了一个用.NET里的GZipStream压缩解压缩gzip文件的小程序. GZipStream在System.IO.Compression底下,使用起来也很简单.虽然GZipStream是Str ...
- 对象回收过程?线程池执行过程? map原理?集合类关系?synchronized 和 volatile ? 同一个类的方法事务传播控制还有作用吗?java 锁
1. 对象回收过程? 可达性分析算法: 如果一个对象从 GC Roots 不可达时,则证明此对象不可用. 通过一系列称为GC ROOTS的对象作为起点,从这些起点往下搜索,搜索走过的路径 称为引用链 ...
- strace使用
统计信息 -c -- count time, calls, and errors for each syscall and report summary 输出结果到文件 -o 例如 strace ...
- java8 forEach Map List[转载]
java8 forEach 在Map和List中的使用 原始的使用 Map<String, Integer> items = new HashMap<>(); items.pu ...