hdu 4550 卡片游戏 贪心
题目链接: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 卡片游戏 贪心的更多相关文章
- hdu 4550 卡片游戏
http://acm.hdu.edu.cn/showproblem.php?pid=4550 贪心 #include <cstdio> #include <cstring> # ...
- 卡片游戏(hdu4550)贪心
卡片游戏 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submi ...
- 【sicily】卡片游戏
卡片游戏 Time Limit: 1sec Memory Limit:32MB Description 桌上有一叠牌,从第一张牌(即位于顶面的牌)开始从上往下依次编号为1~n.当至少还剩两张牌 ...
- Sicily 1931. 卡片游戏
题目地址:1931. 卡片游戏 思路: 纯属数据结构中队列的应用,可以练练手. 具体代码如下: #include <iostream> #include <queue> usi ...
- HDU 4442 Physical Examination(贪心)
HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...
- nyoj905 卡片游戏
卡片游戏 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 小明最近宅在家里无聊,于是他发明了一种有趣的游戏,游戏道具是N张叠在一起的卡片,每张卡片上都有一个数字,数字 ...
- NYOJ 905 卡片游戏
卡片游戏 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描写叙述 小明近期宅在家里无聊.于是他发明了一种有趣的游戏.游戏道具是N张叠在一起的卡片,每张卡片上都有一个数字,数字 ...
- luoguP2123 皇后游戏(贪心)
luoguP2123 皇后游戏(贪心) 题目 洛谷题目chuanso 题解 有一篇好题解,我就懒得推式子了,毕竟打到电脑上还是很难的 牛逼题解传送门 code #include<iostream ...
- Java实现 LeetCode 822 翻转卡片游戏(暴力)
822. 翻转卡片游戏 在桌子上有 N 张卡片,每张卡片的正面和背面都写着一个正数(正面与背面上的数有可能不一样). 我们可以先翻转任意张卡片,然后选择其中一张卡片. 如果选中的那张卡片背面的数字 X ...
随机推荐
- javascript之css常用属性
1. position : 属性值有absolute .fixed.relative absolute:生成绝对定位的元素,相对第一父元素进行定位: fixed : 生成绝对定位的元素,相对于浏览 ...
- php测试代码工具类
<?php error_reporting (E_ALL); ini_set ('display_errors', 'on'); ?> <meta http-equiv=&quo ...
- 获取select值
//Jquery中select的使用 //select获取当前选中的value值 $('#sel').change(function () { var depId = $(this).children ...
- AppScan在项目中的使用流程
AppScan在项目中的使用流程 http://www.docin.com/p-829022229.html
- C C++源代码安全分析工具调研
C C++源代码安全分析工具调研:http://blog.csdn.net/testing_is_believing/article/details/22047107
- C++获取文件长度
ifstream fin("example.txt", ios::binary); fin.seekg(0, ios::end); // 设置指针到文件流尾部 streampos ...
- Matlb中break 和continue 语句
有两个附加语句可以控制while 和for 循环:break 和continue 语句. break 语句可以中止循环的执行和跳到end 后面的第一句执行,而continue 只中止本次循环,然后返回 ...
- ReentrantLock和synchronized的区别
一.ReentrantLock类 ReentrantLock 类实现了 Lock ,它拥有与 synchronized 相同的并发性和内存语义,但是添加了类似锁投票.定时锁等候和可中断锁等候的一些特性 ...
- 在orangepi-PC, ubuntu mini下使用usb wifi(rtl8188cu/rtl8192cu)
本文章针对orangepi-pc下的ubuntu,分两部分,分别介绍在ubuntu14.04(trusty)mini下使用usb无线网卡的方式,以及怎样在ubuntu15.10(wily)mini下正 ...
- HTTPS协议学习总结
目录 一:什么是HTTPS协议?二:客户端与服务端如何建立HTTPS协议连接?三:证书.加密协议.信息摘要.签名算法概念解释与关系梳理四:低版本操作系统作为客户端发送HTTPS失败分析五:参考资料 ...