贪心 HDOJ 4726 Kia's Calculation
/*
这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧!
贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B will have same number of digits 两个数字位数相同
2. which is no larger than 10^6 不是大小,而是长度不超过1e6
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
char s1[MAXN], s2[MAXN]; int main(void) //HDOJ 4726 Kia's Calculation
{
//freopen ("K.in", "r", stdin); int t, cas = ;
int cnt1[], cnt2[], cnt3[]; scanf ("%d", &t);
while (t--)
{
scanf ("%s", &s1);
scanf ("%s", &s2); printf ("Case #%d: ", ++cas); int len = strlen (s1);
if (strcmp (s1, "") == )
{
printf ("%s\n", s2); continue;
}
else if (strcmp (s2, "") == )
{
printf ("%s\n", s1); continue;
} memset (cnt1, , sizeof (cnt1));
memset (cnt2, , sizeof (cnt2));
memset (cnt3, , sizeof (cnt3)); for (int i=; i<len; ++i)
{
cnt1[s1[i]-'']++; cnt2[s2[i]-'']++;
} int ii = , jj = , mx = -;
for (int i=; i<=; ++i)
{
if (cnt1[i] == ) continue;
for (int j=; j<=; ++j)
{
if (cnt2[j] == ) continue;
int tmp = (i + j) % ;
if (tmp > mx)
{
mx = tmp; ii = i; jj = j;
}
}
}
cnt1[ii]--; cnt2[jj]--;
if (!mx)
{
puts (""); continue;
} for (int i=; i>=; --i)
{
for (int j=; j<=; ++j)
{
for (int k=; k<=; ++k)
{
if ((j+k)%==i && cnt1[j] && cnt2[k])
{
int tmp = min (cnt1[j], cnt2[k]);
cnt1[j] -= tmp; cnt2[k] -= tmp;
cnt3[i] += tmp;
}
}
}
} printf ("%d", mx);
for (int i=; i>=; --i)
{
for (int j=; j<cnt3[i]; ++j) printf ("%d", i);
}
puts ("");
} return ;
}
贪心 HDOJ 4726 Kia's Calculation的更多相关文章
- HDU 4726 Kia's Calculation (贪心算法)
		
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
 - HDU 4726 Kia's Calculation(贪心)
		
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
 - ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)
		
DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...
 - HDU 4726 Kia's Calculation(贪心构造)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...
 - hdu 4726 Kia's Calculation
		
思路:刚开始想复杂了. 看解题报告后才知道这题挺简单的,看来还是要多训练啊!!! 单独处理首位的数字,不能为0.其他的就好处理了,从大到小依次找下去就可以了…… 代码如下: #include<i ...
 - K - Kia's Calculation(贪心)
		
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
 - K - Kia's Calculation (贪心)
		
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
 - Kia's Calculation   hdu4726
		
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
 - HDU-4726 Kia's Calculation 贪心
		
题目链接:http://acm.hdu.edu.cn/userstatus.php?user=zhsl 题意:给两个大数,他们之间的加法法则每位相加不进位.现在可以对两个大数的每位重新排序,但是首位不 ...
 
随机推荐
- Valid Pattern Lock(dfs + 暴力)
			
Valid Pattern Lock Time Limit: 2 Seconds Memory Limit: 65536 KB Pattern lock security is genera ...
 - 解决 MySQL Cluster 通过 某一个MySqld节点新建表时,其他 MySqld节点 看不到表内容的问题
			
问题: 总共有 4 个MySqld节点,通过其中的一个节点新建表时,发现其他 MySqld节点 查不到表内容的问题,即表没有同步过来. 解决方案: 主要是因为新建表时,所选的 表引擎 错误导致的,只能 ...
 - 他们在军训,我在搞 OI(一)
			
Day 1 理论上,我现在不应该坐在电脑前打字,因为早在今天上午 6:20 全体新高一同学就坐车前往军(无)训(尽)基(炼)地(狱)了,而今天上午 6:20 我还在被窝里呢…… 没错,我旷掉了军训,然 ...
 - Flatten Binary Tree to Linked List
			
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
 - host
			
#Google Services START209.116.186.241 0.docs.google.com209.116.186.241 0.drive.google.com209.116.186 ...
 - 【转】cas注册后自动登录
			
本文转自:http://denger.iteye.com/blog/805743 1. 关于CAS的介绍不再累述,我想涉及过SSO同学应该都会对该框架所有了解,我们目前项目采用的CAS Server ...
 - maven web项目build失败
			
通过maven build发布web项目到tomcat时报如下异常: [INFO] ---------------------------------------------------------- ...
 - Java中static静态关键字的使用
			
我们可以基于一个类创建多个对象,每个对象都拥有自己的成员,所有成员变量的值是根据对象而存在的,有些时候我们希望一个类的所有对象共享一个成员,这就用到了static静态关键字 被静态关键字修饰的成员属于 ...
 - 【python】pathlib库
			
pathlib在python3.2以上开始默认支持,在python2.7中如果要使用需要安装 pip install pathlib pathlib更多参考资料:http://pathlib.read ...
 - 开源混淆工具ProGuard配置详解及配置实例
			
ProGuard的作用: 1.创建紧凑的代码文档是为了更快的网络传输,快速装载和更小的内存占用. 2.创建的程序和程序库很难使用反向工程. 3.所以它能删除来自源文件中的没有调用的代码 4.充分利 ...