HDU-4726 Kia's Calculation 贪心
题目链接:http://acm.hdu.edu.cn/userstatus.php?user=zhsl
题意:给两个大数,他们之间的加法法则每位相加不进位。现在可以对两个大数的每位重新排序,但是首位不能为0,求两个数相加最大的和。
从最高位开始,考虑和最大贪心,每次从两个大数中找两位a和b,max=(a+b)%10的和最大。如果max有多种组成情况,对于a+b<10的所有情况,我们任意选择一组就可以了。对于存在a'+b'>10的组合情况,如果有a+b<10的组合情况,我们就选择a+b<10的组合情况,因为a'和b'都是大于max的,留下来会更优,否则随便选择一组a'+b'>10的组合情况。。
//STATUS:C++_AC_453MS_2260KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e60;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End char sa[N],sb[N];
int a[N],b[N],cnta[],cntb[],ans[];
int T,n; int main(){
// freopen("in.txt","r",stdin);
int i,j,ca=,t,k,ok;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",sa,sb);
n=strlen(sa);
mem(cnta,);mem(cntb,);
for(i=;i<n;i++){
cnta[sa[i]-'']++;
cntb[sb[i]-'']++;
}
mem(ans,);
int hig=,x,y;
for(i=;i<=;i++){
for(j=;j<=;j++){
if(cnta[i]&&cntb[j]&&(i+j)%>hig){
hig=(i+j)%;
x=i;y=j;
}
}
}
printf("Case #%d: ",ca++);
if(hig==){
printf("0\n");
continue;
}
cnta[x]--,cntb[y]--;
for(t=;t>=;){
ok=;
for(i=;i<=;i++){
if(cnta[i]&&cntb[(t-i+)%]){
ok=;
cnta[i]--;cntb[(t-i+)%]--;
ans[t]++;
break;
}
}
if(!ok)t--;
} printf("%d",hig);
for(i=;i>=;i--){
while(ans[i]--)printf("%d",i);
}
putchar('\n');
}
return ;
}
HDU-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 ...
- HDU 4726 Kia's Calculation(贪心构造)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...
- 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
思路:刚开始想复杂了. 看解题报告后才知道这题挺简单的,看来还是要多训练啊!!! 单独处理首位的数字,不能为0.其他的就好处理了,从大到小依次找下去就可以了…… 代码如下: #include<i ...
- 贪心 HDOJ 4726 Kia's Calculation
题目传送门 /* 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B wi ...
- K - Kia's Calculation (贪心)
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 4726(贪心)
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 ...
随机推荐
- C++:运算符重载函数之友元运算符重载
5.2.2 友元运算符重载函数 运算符重载函数一般采用两种形式定义: 一是定义为它将要操作的类的成员函数(简称运算符重载函数): 二是定义为类的友元函数(简称为友元运算符重载函数). 1.定义友元运算 ...
- python脚本实例001 - 通过列表内容判断输入输出信息
要点总结: 输入输出方法,input().print()方法 list列表应用,list是一种有序的集合,可以随时添加和删除其中的元素. 条件语句if-else应用 #! /usr/bin/pytho ...
- mongodb 常见操作转
Ø Collection聚集集合 1.创建一个聚集集合(table) db.createCollection(“collName”, {size: 20, capped: 5, max: 100}); ...
- WebBrowser.ObjectForScripting 属性的使用
主要是winform窗体中使用了webBrowser控件,webBrowser中调用javascript操作窗体里面的后台方法,使用下面的两句. this.webBrowser1.ObjectForS ...
- Android listView如何判断数据多屏显示 (2012-03-06 09:47:04)
@Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int tot ...
- [转] jQuery Infinite Ajax Scroll(ias) 分页插件介绍
原文链接:http://justflyhigh.com/index.php/articlec/index/index.php?s=content&m=aticle&id=91 Infi ...
- 在Windows下编译FFmpeg详细说明
MinGW:一个可自由使用和自由发布的Windows特定头文件和使用GNC工具集导入库的集合,允许你生成本地的Windows程序而不需要第三方C运行时 MinGW,即 Minimalist GNU F ...
- Darwin Streaming Server Relay Setting
安装完Darwin Streaming Server,就可以使用VLC通过RTSP协议播放流媒体文件了.但是我现在有一个需求,需要将一台DSS(假设为A机)上的媒体文件发送到另一台DSS(假设为B机) ...
- Android开发中常见的设计模式
对于开发人员来说,设计模式有时候就是一道坎,但是设计模式又非常有用,过了这道坎,它可以让你水平提高一个档次.而在android开发中,必要的了解一些设计模式又是非常有必要的.对于想系统的学习设计模式的 ...
- VS2013密匙
在网上找到的,亲测有用: BWG7X-J98B3-W34RT-33B3R-JVYW9