HDU 5375——Gray code——————【dp||讨论】
Gray code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 569 Accepted Submission(s): 337

Now , you are given a binary number of length n including ‘0’ , ’1’ and ‘?’(? means that you can use either 0 or 1 to fill this position) and n integers(a1,a2,….,an) . A certain binary number corresponds to a gray code only. If the ith bit of this gray code is 1,you can get the point ai.
Can you tell me how many points you can get at most?
For instance, the binary number “00?0” may be “0000” or “0010”,and the corresponding gray code are “0000” or “0011”.You can choose “0000” getting nothing or “0011” getting the point a3 and a4.
Each test case begins with string with ‘0’,’1’ and ‘?’.
The next line contains n (1<=n<=200000) integers (n is the length of the string).
a1 a2 a3 … an (1<=ai<=1000)
https://en.wikipedia.org/wiki/Gray_code
#include<bits/stdc++.h>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
const int maxn=1e5+200;
const int INF=0x3f3f3f3f;
int dp[2*maxn][2];
int a[2*maxn];
char str[2*maxn];
int main(){
int t,cnt=0;
scanf("%d",&t);
while(t--){
scanf("%s",str+1);
int len=strlen(str+1);
for(int i=1;i<=len;i++){
scanf("%d",&a[i]);
dp[i][1]=dp[i][0]=-INF;
}
if(str[1]=='0'){
dp[1][0]=0;
}else if(str[1]=='1'){
dp[1][1]=a[1];
}else{
dp[1][0]=0;
dp[1][1]=a[1];
}
for(int i=2;i<=len;i++){
if(str[i]=='0'){
dp[i][0]=max(dp[i-1][0],dp[i-1][1]+a[i]);
}else if(str[i]=='1'){
dp[i][1]=max(dp[i-1][0]+a[i],dp[i-1][1]);
}else {
dp[i][1]=max(dp[i-1][0]+a[i],dp[i-1][1]);
dp[i][0]=max(dp[i-1][1]+a[i],dp[i-1][0]);
}
}
printf("Case #%d: %d\n",++cnt,max(dp[len][1],dp[len][0]));
}
return 0;
}
讨论:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e6+200;
const int INF=0x3f3f3f3f;
char str[2*maxn];
int a[maxn*2];
int main(){
int t,cnt=0;
scanf("%d",&t);
while(t--){
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<len;i++)
scanf("%d",&a[i]);
int sum=0,pos=len,flag=-1;
int i;
for(i=len-1;i>=0;i--){ //逆序处理
if(str[i]=='0'){
if(flag==1){ //如果右边有1
sum+=a[pos];
}
flag=0; //表示当前是0
pos=i;
continue;
}else if(str[i]=='1'){
if(flag==0){
sum+=a[pos];
}
flag=1;
pos=i;
continue;
}else{ //是问号
int num=0,sumv,minv;
if(flag!=-1){
minv=a[pos],sumv=a[pos];
}
else {
minv=INF,sumv=0;
}
int j;
for(j=i;j>=0;j--){
if(str[j]=='0'){
if(flag==1){
if(num%2==1){ //如果问号两边不同,且中间的问号为奇数个,舍弃这段中的最小值
sum+=(sumv-minv);
}else {
sum+=sumv;
}
}else if(flag==0){
if(num%2==1){ //如果两边相同,且中间为奇数个,加上这段所有值
sum+=sumv;
}else{
sum+=(sumv-minv);
} }else{
sum+=sumv;
}
flag=-1;
pos=len;
i=j+1;
break;
}else if(str[j]=='1'){
if(flag==0){
if(num%2==1){
sum+=(sumv-minv);
}else{
sum+=sumv;
}
}else if(flag==1){
if(num%2==0){
sum+=(sumv-minv);
}else{
sum+=sumv;
}
}else{
sum+=sumv;
}
flag=-1;
pos=len;
i=j+1;
break;
}else {
num++;
sumv+=a[j];
if(minv>a[j]){
minv=a[j];
}
}
}
if(j==-1){ //边界处理,比较恶心
i=0;
if(flag==-1 )
sum+=sumv;
else if (flag==1){
if(num%2==0){
sum+= sumv;
}
else sum+=(sumv-minv);
}else {
if(num%2==1){
sum+=sumv;
}else{
sum+=(sumv-minv);
}
}
}
}
}
if(str[0]=='1'&&i==-1){ //字串第一位
sum+=a[0];
}
printf("Case #%d: %d\n",++cnt,sum);
}
return 0;
}
HDU 5375——Gray code——————【dp||讨论】的更多相关文章
- hdu 5375 Gray code dp
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; cons ...
- HDU 5375 Gray code (简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Oth ...
- hdu 5375 - Gray code(dp) 解题报告
Gray code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- 2015 Multi-University Training Contest 7 hdu 5375 Gray code
Gray code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 5375 Gray code(2015年多校联合 动态规划)
题目连接 : 传送门 题意: 给定一个长度为的二进制串和一个长度为n的序列a[],我们能够依据这个二进制串得到它的Gray code. Gray code中假设第i项为1的话那么我们就能够得到a[i] ...
- hdu 5375 Gray code 【 dp 】
dp[i][j]表示第i位取j的时候取得的最大的分数 然后分s[i]是不是问号,s[i-1]是不是问号这大的四种情况讨论 #include<cstdio> #include<cstr ...
- HDU 5375 Gray code(DP)
题意:给一串字符串,里面可能出现0,1,?,当中问号可能为0或1,将这个二进制转换为格雷码后,格雷码的每位有一个权值,当格雷码位取1时.加上该位权值,求最大权值和为多少. 分析:比赛的时候愚了.竟然以 ...
- HDU 5375 Gray code 格雷码(水题)
题意:给一个二进制数(包含3种符号:'0' '1' '?' ,问号可随意 ),要求将其转成格雷码,给一个序列a,若转成的格雷码第i位为1,则得分+a[i].求填充问号使得得分最多. 思路:如果了 ...
- HDU 5375 Gray code
题意:给出一个二进制数,其中有些位的数字不确定,对于所有对应的格雷码,与一个序列a对应,第i位数字为1时得分a[i],求最大的得分. 解法:一个二进制数x对应的格雷码为x ^ (x >> ...
随机推荐
- pch文件的创建与配置
1.ios中pch文件的创建与配置 1.1 ios中pch文件的创建与配置 1.2 全局宏定义标志的配置 2.宏定义 这里放的主要是开发中常用的宏定义. /** 动态的字符串格式化宏 */ #defi ...
- 常用SQL语句及在node中使用MySQL
摘要:一些重要的SQL命令 SELECT - 从数据库中提取数据 UPDATE - 更新数据库中的数据 DELETE - 从数据库中删除数据 INSERT INTO - 向数据库中插入新数据 CREA ...
- JBOSS在win7环境下启动run.bat无反应
今天从隔壁机器拷贝了一份Jboss,却发现启动无任何反应. 仔细对比了jdk jboss的各项参数发现都是相同,无奈之下,检查run.bat文件 发现时在此句出现前后 无反应: "%JAVA ...
- 牛客国庆集训day6 B Board (模拟标记思维或找规律或分块???)
链接:https://www.nowcoder.com/acm/contest/206/B来源:牛客网 题目描述 恬恬有一个nx n的数组.她在用这个数组玩游戏: 开始时,数组中每一个元素都是0. 恬 ...
- 圆环进度css
看效果先:http://sandbox.runjs.cn/show/b6bmksvn 参考: jquery圆环百分比进度条制作 CSS clip:rect矩形剪裁功能及一些应用介绍 CSS clip: ...
- Js 向json对象中添加新元素
即:var json={a:1,b:2} json.c=3 添加新元素直接使用赋值就行了
- 老男孩python作业9-简单的商城页面布局
利用HTML相关知识编写下面的网页: 核心代码如下: <!DOCTYPE html> <html lang="en"> <head> <m ...
- 工具类_SharedPreferencesUtils
import android.app.Application;import android.content.Context;import android.content.SharedPreferenc ...
- python+splinter实现12306网站刷票并自动购票流程
python+splinter实现12306网站刷票并自动购票流程 通过python+splinter,实现在12306网站刷票并自动购票流程(无法自动识别验证码). 此类程序只是提高了12306网站 ...
- 对称加密中的ECB模式&CBC模式
ECB模式: CBC模式: 所有的迭代模式: