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] ...
随机推荐
- location.href 实现点击下载功能
如果页面上要实现一个点击下载的功能,传统做法是使用一个 a 标签,然后将该标签的 href 属性地址指向下载文件在服务端的地址(相对地址或者绝对地址),比如这样: 能这样实现是因为,在浏览器地址栏输入 ...
- iostat命令详解
iostat iostat用于输出CPU和磁盘I/O相关的统计信息. 命令格式: iostat [ -c | -d ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ devic ...
- jquery 使用方法(转)
原文: http://www.cnblogs.com/Chenfengtao/archive/2012/01/12/2320490.html jQuery是目前使用最广泛的javascript函数库. ...
- ContentProvider详解
作用:把一个App中的数据库通过Url的形式共享出来,供其他App使用. 首先在App1中创建一个数据库,用SQLiteOpenHelper public class MyOpenHelper ext ...
- ElasticSearch入门系列(三)文档,索引,搜索和聚合
一.文档 在实际使用中的对象往往拥有复杂的数据结构 Elasticsearch是面向文档的,这意味着他可以存储整个对象或文档,然而他不仅仅是存储,还会索引每个文档的内容使之可以被搜索,在Elastic ...
- iOS常用---NSString,NSMutabuleString
普通的创建字符串: NSString *string =[[NSString alloc]init]; //创建一个空的字符串,即 @“” 一 .字符串读取本地文件: 参数一: 文件地址 参数二: 文 ...
- jquery slide使用总结
.slideUp([duration][,complete])--目标元素向上滑入隐藏: .slideDown([duration][,complete])--目标元素向下滑出显示: .slideTo ...
- SQL Server数据库转换成oracle
来源:http://blog.csdn.net/hzfu007/article/details/6182151 经常碰到需要把sql server的数据迁移到Oracle的情况. 在网上查找一下,有很 ...
- JS实时定位
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- URL详解与URL编码
作为前端,每日与 URL 打交道是必不可少的.但是也许每天只是单纯的用,对其只是一知半解,随着工作的展开,我发现在日常抓包调试,接口调用,浏览器兼容等许多方面,不深入去理解URL与URL编码则会踩到很 ...