hdu 4726(贪心)
Kia's Calculation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3291 Accepted Submission(s): 703
Ghee is teaching Kia how to calculate the sum of two integers. But Kia
is so careless and alway forget to carry a number when the sum of two
digits exceeds 9. For example, when she calculates 4567+5789, she will
get 9246, and for 1234+9876, she will get 0. Ghee is angry about this,
and makes a hard problem for her to solve:
Now Kia has two integers A
and B, she can shuffle the digits in each number as she like, but
leading zeros are not allowed. That is to say, for A = 11024, she can
rearrange the number as 10124, or 41102, or many other, but 02411 is not
allowed.
After she shuffles A and B, she will add them together, in her own way. And what will be the maximum possible sum of A "+" B ?
For each test case there are two lines. First line has the number A, and the second line has the number B.
Both A and B will have same number of digits, which is no larger than 106, and without leading zeros.
5958
3036
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define LL long long
using namespace std;
const int N = ;
char str1[N],str2[N];
int num1[],num2[];
int res[N];
int main()
{
int tcase,t = ;
scanf("%d",&tcase);
while(tcase--)
{ scanf("%s%s",str1,str2);
if(strcmp(str1,"")==){
printf("Case #%d: ",t++);
printf("%s\n",str2);
continue;
}
if(strcmp(str2,"")==){
printf("Case #%d: ",t++);
printf("%s\n",str1);
continue;
}
int len = strlen(str1);
memset(num1,,sizeof(num1));
memset(num2,,sizeof(num2));
for(int i=; i<len; i++)
{
num1[str1[i]-'']++;
num2[str2[i]-'']++;
}
int high = -,x,y;
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
if(num1[i]&&num2[j]&&high<(i+j)%)
{
x = i;
y = j;
high = (i+j)%;
}
}
}
num1[x]--;
num2[y]--;
int cnt = ,zero = ;
res[cnt++] = high;
if(high==) zero++;
printf("Case #%d: ",t++);
if(zero){
printf("0\n");
continue;
}
for(int l=; l<len; l++)
{
/*
TLE
for(int i=0; i<=9; i++)
{
for(int j=0; j<=9; j++)
{
if(num1[i]&&num2[j]&&MAX<(i+j)%10)
{
x = i;
y = j;
MAX = (i+j)%10;
}
}
}*/
bool flag = true;
for(int i=;i>=&&flag;i--){
for(int j=;j<=&&flag;j++){
if(i-j<&&num1[j]&&num2[i-j+]){
num1[j]--;
num2[i-j+]--;
res[cnt++] = i;
flag = false;
}else if(i-j>=&&num1[j]&&num2[i-j]){
num1[j]--;
num2[i-j]--;
res[cnt++] = i;
flag = false;
}
}
} }
for(int i=;i<cnt;i++){
printf("%d",res[i]);
}
printf("\n");
}
return ;
}
hdu 4726(贪心)的更多相关文章
- HDU 4726 Kia's Calculation(贪心构造)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...
- 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 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
- hdu 1735(贪心) 统计字数
戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...
- hdu 4974 贪心
http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...
- hdu 4982 贪心构造序列
http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...
随机推荐
- scala 高级编程
一.函数式编程 Scala中的函数可以独立存在, 不需要依赖任 何类和对象 def 放在类中就是方法:放在外边就是函数 1.将函数赋值给变量 Scala中的函数是一等公民, 可以独立定义, 独立存在 ...
- 【数学】【背包】【NOIP2018】P5020 货币系统
传送门 Description 在网友的国度中共有 \(n\) 种不同面额的货币,第 \(i\) 种货币的面额为 \(a[i]\),你可以假设每一种货币都有无穷多张.为了方便,我们把货币种数为 \(n ...
- ibatis中:org.springframework.jdbc.UncategorizedSQLException:异常
SQL 查询语句异常,可能是你的查询语句写错了,或者是你的映射的类和或数据中与表不对应,检查你的映射配置文件.
- 洛谷P2253 好一个一中腰鼓!
题目背景 话说我大一中的运动会就要来了,据本班同学剧透(其实早就知道了),我萌萌的初二年将要表演腰鼓[喷],这个无厘头的题目便由此而来. Ivan乱入:“忽一人大呼:‘好一个安塞腰鼓!’满座寂然,无敢 ...
- 使用cron命令配置定时任务(cron jobs)
原文 http://www.cnblogs.com/end/archive/2012/02/21/2361741.html 开机就启动cron进程的设置命令:chkconfig --add crond ...
- TestNG指南
转载自:http://blog.csdn.net/bigapplestar/article/details/7300137 今天突然收到通知,统一改用TestNG写测试用例,开始查这方面的资料,学习一 ...
- 解决问题Can’t connect to local MySQL server through socket
不幸遇到MySQL出现ERROR 2002 (HY000): Can’t connect to local mysql server through socket ‘/tmp/mysql.sock’错 ...
- CF821 A. Okabe and Future Gadget Laboratory 水
Link 题意:询问n X n中非1数是否能够由同行同列中分别取两个数做和得到. 思路:水题. /** @Date : 2017-07-03 16:23:18 * @FileName: A.cpp * ...
- How to Evaluate Machine Learning Models, Part 4: Hyperparameter Tuning
How to Evaluate Machine Learning Models, Part 4: Hyperparameter Tuning In the realm of machine learn ...
- JavaScript设置粘贴板
设置复制 document.body.oncopy = function(){ alert('不许复制'); return false; }; 设置粘贴 document.getElementById ...