CF 435B Pasha Maximizes(贪心)
题目链接: [传送门][1]
Pasha Maximizes
time limit per test:1 second memory limit per test:256 megabytes
Description
Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.
Help Pasha count the maximum number he can get if he has the time to make at most k swaps.
Input
The single line contains two integers a and k (1 ≤ a ≤ 1018; 0 ≤ k ≤ 100).
Output
Print the maximum number that Pasha can get if he makes at most k swaps.
Sample Input
1990 1
300 0
1034 2
Sample Output
9190
300
3104
9907000008001234
解题思路:
题目大意:给你一个整数,为交换相邻的两个数字k次能达到的最大数字的值
简单贪心,暴力从头开始枚举,选取从枚举位置开始前k个字符中最大的,使之前移。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
char str[100];
int ans[100],K,maxx,pos;
memset(str,0,sizeof(str));
memset(ans,0,sizeof(ans));
while(~scanf("%s %d",str,&K))
{
int len = strlen(str);
for (int i = 0; i < len; i++)
{
ans[i] = str[i] - '0';
}
bool flag = false;
for (int i = 0; i < len; i++)
{
if (!K)
break;
maxx = 0;
for (int j = i; j <= i + K && j < len; j++)
{
if (maxx < ans[j])
{
maxx = ans[j];
pos = j;
flag = true;
}
}
for (int j = pos; j > i; j--)
{
int tmp = ans[j];
ans[j] = ans[j-1];
ans[j-1] = tmp;
}
if (flag)
{
K = K - (pos - i);
flag = false;
}
}
for (int i = 0; i < len; i++)
{
printf("%d",ans[i]);
}
printf("\n");
memset(str,0,sizeof(str));
memset(ans,0,sizeof(ans));
}
return 0;
}
CF 435B Pasha Maximizes(贪心)的更多相关文章
- Codeforces 435B. Pasha Maximizes
简单贪心.... B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces 435 B Pasha Maximizes【贪心】
题意:给出一串数字,给出k次交换,每次交换只能交换相邻的两个数,问最多经过k次交换,能够得到的最大的一串数字 从第一个数字往后找k个位置,找出最大的,往前面交换 有思路,可是没有写出代码来---sad ...
- CF 557B(Pasha and Tea-贪心)
B. Pasha and Tea time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CF 115B Lawnmower(贪心)
题目链接: 传送门 Lawnmower time limit per test:2 second memory limit per test:256 megabytes Description ...
- CF Soldier and Badges (贪心)
Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- CF Anya and Ghosts (贪心)
Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- cf 853 A planning [贪心]
题面: 传送门 思路: 一眼看得,这是贪心[雾] 实际上,我们要求的答案就是sigma(ci*(ti-i))(i=1~n),这其中sigma(ci*i)是确定的 那么我们就要最小化sigma(ci*t ...
- codeforces 435 B. Pasha Maximizes 解题报告
题目链接:http://codeforces.com/problemset/problem/435/B 题目意思:给出一个最多为18位的数,可以通过对相邻两个数字进行交换,最多交换 k 次,问交换 k ...
- CF 1082E Increasing Frequency(贪心)
传送门 解题思路 贪心.对于一段区间中,可以将这段区间中相同的元素同时变成\(c\),但要付出的代价是区间中等于\(c\)的数的个数,设\(sum[i]\)表示等于\(c\)数字的前缀和,Max[i] ...
随机推荐
- 写个PHP框架吧
肯定会问:现在的PHP框架那么多了,为什么还要写一个PHP框架呢? 1.时代:PHP7来了,现在的所有框架都是基于PHP5.x的.到时候PHP7正式推广出来,现有的框架都不能发挥PHP7的最大性能优势 ...
- 在项目中代替DevExpress(一)
从Delphi时代开始一直都是DevExpress系列控件的忠实用户,到现在已经有10多个年头了.DevExpress里面的控件基本从头到尾都用过一次,而且也开发过很多基于DevExpress的子控件 ...
- FPGA中的INOUT接口和高阻态
除了输入输出端口,FPGA中还有另一种端口叫做inout端口.如果需要进行全双工通信,是需要两条信道的,也就是说需要使用两个FPGA管脚和外部器件连接.但是,有时候半双工通信就能满足我们的要求,理论上 ...
- 如何在Vue2中实现组件props双向绑定
Vue学习笔记-3 前言 Vue 2.x相比较Vue 1.x而言,升级变化除了实现了Virtual-Dom以外,给使用者最大不适就是移除的组件的props的双向绑定功能. 以往在Vue1.x中利用pr ...
- Scala入门详解
object作为Scala中的一个关键字,相当于Java中的public static class这样的一个修饰符,也就说object中的成员都是静态的! 所以我们在这个例子中的main方法是静态的, ...
- [POJ2104]K-th Number
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 34048 Accepted: 10810 Ca ...
- [转]java 输出流转输入流
ByteArrayOutputStream.toByteArray ByteArrayInputStream StringWriter.toString StringReader 字符流和二进制流是j ...
- PotPlayer为播放而生的专业播放器
韩国,比较牛逼的视频播放器.专注与本地视频播放,值得拥有.... 免费下载:http://yunpan.cn/cmZ5ELC6DTI8Y 访问密码 4bf1
- [cross domain] four approachs to cross domain in javascript
four approachs can cross domain in javascript 1.jsonp 2.document.domain(only in frame and they have ...
- Bete冲刺第三阶段
Bete冲刺第三阶段 今日工作: web: 检索了各类资料,今日暂时顺利解决了hibernate懒加载异常的问题,采用的凡是也比较简单就是添加了一个OpenSessionInViewFilter的过滤 ...