Google Code Jam 2016 Round 1B B
题意:给出两个数字位数相同,分别中间有若干位不知道,用问号表示。现在要求补全这两个数字,使得差值的绝对值最小,多解则取第一个数字的值最小的,再多解就取第二个数字最小的。
分析:
类似数位dp,但是很多状态可以直接得出最终解,个别状态需要状态转移。
我们从高位到低位依次确定两个数的每个位是几。一旦确定了两个数的一个位不一样,则可以立即将小的一方的后续问号全部写9,大的一方后续问号全部写0。这样才能让差值最小。
那我们观察每个位的时候要如何确定其值呢?分如下几种情况。
1.两个数的该位都是问号,那么分三种情况:
1.1 都标为0,看下一个位。
1.2&1.3 将一位标为1,另一个位标为0,并更新答案,终结状态。(这表示我们主动在高位制造了一个差值以便后面取得整体差值最小。例如:?0?,?9?,答案是100,099)
2.两个数的该位都不是问号,若相等,继续看下一位。若不等,则标出后面问号,更新答案,终结状态。
3.两个数的该位有一个是问号,那么这个这与第一种情况类似,分三种情况处理。
3.1 标为与该位相等,看下一个位。
3.2 标为该位减1,赋值后续问号,更新答案,终结状态。
3.3 标为该位加1,赋值后续问号,更新答案,终结状态。
就这么多情况。值得注意的是,虽然有些时候进行了状态转移(看下一个位)。但是并不需要搜索和回溯。因为每个状态的多个分支中,只有一个是状态转移,其他的都是最终态。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std; #define d(x) const int MAX_LEN = ; char st[][MAX_LEN];
char ans[][MAX_LEN];
char temp[][MAX_LEN];
long long diff;
int n; void input()
{
scanf("%s", st[]);
scanf("%s", st[]);
n = strlen(st[]);
} long long get_value(char st[])
{
long long ten = ;
long long ret = ;
for (int i = n - ; i >= ; i--)
{
ret += (st[i] - '') * ten;
ten *= ;
}
return ret;
} bool ok(long long temp_diff)
{
if (temp_diff > diff)
return false;
if (temp_diff < diff)
return true;
if (strcmp(ans[], temp[]) > )
return true;
if (strcmp(ans[], temp[]) < )
return false;
return strcmp(ans[], temp[]) > ; } void update()
{
long long a = get_value(temp[]);
long long b = get_value(temp[]);
long long temp_diff = abs(a - b);
if (ok(temp_diff))
{
diff = temp_diff;
strcpy(ans[], temp[]);
strcpy(ans[], temp[]);
}
} void make(int index, int a, int b)
{
if (a < || b < || a > || b > )
return; strcpy(temp[], st[]);
strcpy(temp[], st[]); temp[][index] = a + '';
temp[][index] = b + ''; int ch_a = '';
int ch_b = '';
if (a > b)
swap(ch_a, ch_b);
for (int i = index + ; i < n; i++)
{
if (temp[][i] == '?')
temp[][i] = ch_a;
if (temp[][i] == '?')
temp[][i] = ch_b;
}
d(printf("a=%d\n", a));
d(printf("b=%d\n", b));
d(puts(temp[]));
d(puts(temp[]));
update();
} void work()
{
diff = 1LL << ;
for (int i = ; st[][i]; i++)
{
if (st[][i] == st[][i] && st[][i] != '?')
{
continue;
}
if (st[][i] == st[][i] && st[][i] == '?')
{
make(i, , );
make(i, , );
st[][i] = st[][i] = '';
continue;
}
//reach here means st[0][i] != st[1][i]
if (st[][i] != '?' && st[][i] != '?')
{
make(i, st[][i] - '', st[][i] - '');
return;
}
//reach here means only one of them is ?.
if (st[][i] == '?')
{
make(i, st[][i] - '' + , st[][i] - '');
make(i, st[][i] - '' - , st[][i] - '');
st[][i] = st[][i];
}
if (st[][i] == '?')
{
make(i, st[][i] - '', st[][i] - '' + );
make(i, st[][i] - '', st[][i] - '' - );
st[][i] = st[][i];
} }
make(n - , st[][n - ] - '', st[][n - ] - '');
} int main()
{
int t;
scanf("%d", &t);
int case_num = ;
while (t--)
{
case_num++;
printf("Case #%d: ", case_num);
input();
work();
printf("%s %s\n", ans[], ans[]);
}
return ;
}
Google Code Jam 2016 Round 1B B的更多相关文章
- Google Code Jam 2016 Round 1B Problem C. Technobabble
题目链接:https://code.google.com/codejam/contest/11254486/dashboard#s=p2 大意是教授的学生每个人在纸条上写一个自己的topic,每个to ...
- Google Code Jam 2010 Round 1B Problem B. Picking Up Chicks
https://code.google.com/codejam/contest/635101/dashboard#s=p1 Problem A flock of chickens are runn ...
- Google Code Jam 2010 Round 1B Problem A. File Fix-it
https://code.google.com/codejam/contest/635101/dashboard#s=p0 Problem On Unix computers, data is s ...
- Google Code Jam 2016 Round 1C C
题意:三种物品分别有a b c个(a<=b<=c),现在每种物品各选一个进行组合.要求每种最和最多出现一次.且要求任意两个物品的组合在所有三个物品组合中的出现总次数不能超过n. 要求给出一 ...
- Google Code Jam 2014 Round 1B Problem B
二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring& ...
- [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...
- Google Code Jam 2010 Round 1C Problem A. Rope Intranet
Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...
- [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha
Problem B. Cookie Clicker Alpha Introduction Cookie Clicker is a Javascript game by Orteil, where ...
- [Google Code Jam (Qualification Round 2014) ] A. Magic Trick
Problem A. Magic Trick Small input6 points You have solved this input set. Note: To advance to the ...
随机推荐
- git 指令
單獨看這個 commit id 做了什麼修改 git show CommitID or git diff CommitID^!
- sql 触发器删除操作
create trigger CheckDelete on 表 for delete as ) select @state=isnull(字段,'') from deleted if (@state& ...
- 分享10条PHP性能优化的小技巧,帮助你更好的用PHP开发:
1. foreach效率更高,尽量用foreach代替while和for循环. 2. 循环内部不要声明变量,尤其是对象这样的变量. 3. 在多重嵌套循环中,如有可能,应当将最长的循环放在内层,最短循环 ...
- mui 动态加载数据出现的问题处理 (silder )
mui-slider 问题:动态给mui的图片轮播添加图片,轮播不滚动. 解决:最后把滚动轮播图片的mui(".mui-slider").slider({interval: 300 ...
- bzoj2702[SDOI2012]走迷宫
题意:给你一个有向图,点数10000,边数1000000,SCC大小不超过100(按数据范围的写法只有第三部分数据满足这个条件,不过第二部分数据并没有出现大小大于100个点的SCC,我是用数组大小为1 ...
- ElasticSearch中bulkProcesser使用
初次接触es,可能对增删改查很熟悉,以为能为得心应手,本次应用场景为 数据库变更一条记录,会触发更新es中的数据,每秒并发大概30条左右,测试环境一切工作正常(数据量较少),上线后发现日志中很多类似于 ...
- 理解Compressed Sparse Column Format (CSC)
最近在看<Spark for Data Science>这本书,阅读到<Machine Learning>这一节的时候被稀疏矩阵的存储格式CSC给弄的晕头转向的.所以专门写一篇 ...
- iOS - 滑屏方案
参考自:iOS开发- 通过ChildViewCotroller ViewController容器 产品增加新的版面,类似于网易新闻,百度新闻,腾讯新闻等新闻客户端首页多屏幕滑屏切换,找了一些开源代码研 ...
- 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(四)
SpringSecurity(1) 其实啊,这部分我是最不想写的,因为最麻烦的也是这部分,真的是非常非常的麻烦.关于SpringSecurity的配置,让我折腾了好半天,网上的配置方式一大把,但总有一 ...
- JAVA中保留小数的多种方法
// 方式一:double f = 3.1516;BigDecimal b = new BigDecimal(f);double f1 = b.setScale(2, BigDecimal.ROUND ...