题目链接: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. PHP加速器

    转http://www.vpser.net/opt/apc-eaccelerator-xcache.html 一.PHP加速器介绍         PHP加速器是一个为了提高PHP执行效率,从而缓存起 ...

  2. jemalloc源码结构分析(一):内存申请处理过程

    一.5种malloc方法 1)tcache_alloc_small 2)arena_malloc_small 3)tcache_alloc_large 4)arena_malloc_large 5)h ...

  3. C C++源代码安全分析工具调研

    C C++源代码安全分析工具调研:http://blog.csdn.net/testing_is_believing/article/details/22047107

  4. xsl输出html代码 非闭合

    ``` </div> <div class="row-fluid"> ···

  5. [转]为什么python标准库没有实现链表

    实际上刚开始学习一些高级语言的时候我也有同样的疑问,而且即使有链表对应物的语言,链表常常也很少被实际使用.如果是在国外听数据结构的课,老师一般会警告你这只是一个理论概念,实际应用应该实际考察,在通常情 ...

  6. [转]在.Net中使用Oracle的表类型和对象类型

    本文转自:http://www.cnblogs.com/studyzy/archive/2010/10/13/1850161.html 在一般的数据存取操作过程中,如果要对一个主表和对应的子表进行插入 ...

  7. .Net 中表达式的转换

    .Net 中表达式的转换 如: a>0  && (c>a || a <b ) || (a>b || c>1)    转换后  (((a > 0) a ...

  8. git命令(流程)

    1,配置你的信息: git config --global user.name 你的名字 git config --global user.email 你的邮箱 --global 表示所有git仓库都 ...

  9. ASP获取当前页面带参数的网址(URL地址)的方法

    '获取当前Url参数的函数 Function GetUrl() Dim ScriptAddress,Servername,qs ScriptAddress = CStr(Request.ServerV ...

  10. android NDK 笔记

    *************************************************华丽的分割线********************************************* ...