题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4550

题意:有n(n <= 100)个0~9之间的卡片,从左往右将卡片放到之前的卡片最左边或者最右边;使得最后得到的数值最小,且无前导0;

input:

4

0101

2342001

9876105432

9876543210

output:

1001

1002234

1678905432

1234567890

思路:如果没有0,则直接用双指针,模拟前后插入值即可;但是由于存在0,所以需要找出最小的非零0,使得最前面以mn开头,之后是可填入的若干个0.所谓可填入的即指在最后一个mn填入之前的0,这就需要记录mn的个数,每次插入mn时,--cnt;当已经在前面填入了0时,将最后最后一个mn时填入会使得值最小,或者是在没有填入0时,但只剩下一个mn时填入mn,这样之后只能在最后加值了;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1|1
typedef pair<int,int> PII;
#define A first
#define B second
#define MK make_pair
typedef __int64 ll;
template<typename T>
void read1(T &m)
{
T x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
#define N 222
int f[N],len,mn,l,r,flag,cnt;
char s[N];
void solve(int a)
{
if(flag){
f[r++] = a;
return ;
}
if(a <= f[l]){// 没填入0,但是mn用完了
f[--l] = a;
if(a == mn && --cnt == ) flag = ;
}
else if(a == mn && cnt-- == ){//前面为0,但是只剩下一个mn了,填入
flag = ,f[--l] = a;
}
else f[r++] = a;
}
int main()
{
int T;
read1(T);
while(T--){
gets(s);
len = strlen(s),mn = ;
l = r = ;f[l] = ;cnt = ;
rep0(i,,len)if(s[i] != '')
if(mn > s[i]-'') mn = s[i]-'',cnt = ;
else if(mn == s[i] - '') cnt++;
flag = ;
rep0(i,,len)
solve(s[i]-'');
rep0(i,l,r) putchar(''+f[i]);
puts("");
}
return ;
}

hdu 4550 卡片游戏 贪心的更多相关文章

  1. hdu 4550 卡片游戏

    http://acm.hdu.edu.cn/showproblem.php?pid=4550 贪心 #include <cstdio> #include <cstring> # ...

  2. 卡片游戏(hdu4550)贪心

    卡片游戏 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submi ...

  3. 【sicily】卡片游戏

    卡片游戏  Time Limit: 1sec    Memory Limit:32MB Description 桌上有一叠牌,从第一张牌(即位于顶面的牌)开始从上往下依次编号为1~n.当至少还剩两张牌 ...

  4. Sicily 1931. 卡片游戏

    题目地址:1931. 卡片游戏 思路: 纯属数据结构中队列的应用,可以练练手. 具体代码如下: #include <iostream> #include <queue> usi ...

  5. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  6. nyoj905 卡片游戏

    卡片游戏 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 小明最近宅在家里无聊,于是他发明了一种有趣的游戏,游戏道具是N张叠在一起的卡片,每张卡片上都有一个数字,数字 ...

  7. NYOJ 905 卡片游戏

    卡片游戏 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描写叙述 小明近期宅在家里无聊.于是他发明了一种有趣的游戏.游戏道具是N张叠在一起的卡片,每张卡片上都有一个数字,数字 ...

  8. luoguP2123 皇后游戏(贪心)

    luoguP2123 皇后游戏(贪心) 题目 洛谷题目chuanso 题解 有一篇好题解,我就懒得推式子了,毕竟打到电脑上还是很难的 牛逼题解传送门 code #include<iostream ...

  9. Java实现 LeetCode 822 翻转卡片游戏(暴力)

    822. 翻转卡片游戏 在桌子上有 N 张卡片,每张卡片的正面和背面都写着一个正数(正面与背面上的数有可能不一样). 我们可以先翻转任意张卡片,然后选择其中一张卡片. 如果选中的那张卡片背面的数字 X ...

随机推荐

  1. Lipo Error!! can't open input file

    参考文章:http://stackoverflow.com/questions/17348912/lipo-error-cant-open-input-file I got it to Work, i ...

  2. java.lang.NoSuchFieldError: RAW_XML_FILE_HEADER,调用XWPFTemplate动态合并生成一个新的docx文档时报错

    在使用 org.apache.poi 对office文件  根据表单内容和已上次的附件 动态合并成一个新的文档时,本地调试完全ok 但是发布倒Linux环境上就老是报这个错误java.lang.NoS ...

  3. Swift数据类型

    1.Swift中常用数据类型:首字母大写 Int.Float.Double.Bool.Character.String Array.Dictionary.元组类型(Tuple).可选类型Optiona ...

  4. JAVA_Reflection

    package com.qf.reflection; import java.lang.reflect.Constructor; import java.lang.reflect.Field; imp ...

  5. 关于Arrays类总结

    Arrays是java中的工具类,其中所有的方法都是static.类名就可以直接调用其中的方法. 本文部分引用自: http://www.importnew.com/8952.html Arrays. ...

  6. mongodb学习相关网址

    1.MongoDB官网 https://www.mongodb.org 2.MongoDB教程 http://www.yiibai.com/mongodb 3.MongoDB教程http://www. ...

  7. php中的匿名函数(Anonymous functions)和闭包函数(closures)

    一:匿名函数 (在php5.3.0 或以上才能使用) php中的匿名函数(Anonymous functions), 也叫闭包函数(closures), 允许指定一个没有名称的函数.最常用的就是回调函 ...

  8. 关于sharepoint 2010无法显示用户中文名的解决方法和详细剖析

    相信这个问题许多做sharepoint的朋友都曾经遇到过,就是本来很正常的中文用户名莫名其妙的变成了“域名\账号”,我本人也遇到过好多次,每次都是百度谷歌一下草草解决问题,始终也没真正去弄明白是怎么回 ...

  9. JavaScript 零散知识点1 (正则表达式+定时器+hover)

    1.clear:both清楚浮动影响//css中 2.正则表达式 search方法 :指明是否存在相应的匹配,如找到一个返回一个整数值,表明这个匹配距离字符串开始的偏移位置,如果没有找到匹配返回-1f ...

  10. 关于 I/O Wait

    I/O wait is a per-CPU performance metric showing time spent idle, when there are threads on the CPU ...