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] ...
随机推荐
- 我所认识的javascript正则表达式
前言 如果说这是一篇关于正则表达式的小结,我更愿意把它当做一个手册. 目录:(点击可直达) RegExp 三大方法(test.exec.compile) String 四大护法(search.matc ...
- <实训|第十三天>linux中ACL权限控制以及磁盘配额,附编译属于自己的linux内核
[root@localhost~]#序言 首先讲讲昨天关于缩容失败,开不机的解决方法:ACL权限也算是一个很重要的知识点,不难,但是很实用:磁盘配额一般不需要自己弄,但是要懂得原理.剩下的就是编译属于 ...
- social emotion computing-感情的分类
第八节 情感的分类 人的情感复杂多样,可以从不同的观察角度进行分类.由于情感的核心内容是价值,因此人的情感主要必须根据它所反映的价值关系的运动与变化的不同特点来进行分类. 1.根据价值的正负变化方向 ...
- CSS Bug
父子标签间用margin的问题,表现在有时除IE(6/7)外的浏览器子标签margin转移到了父标签上,IE6&7下没有转移.测试代码: <body> <style type ...
- Java Little Knowledge
1.Constructor running order of Base class and Derived class This is Alibaba's audition problem. clas ...
- xshell4|5远程连接工具
志同道合,方能谈天说地! 对比其他的工具,对于功能来说xshell是比较厉害的.有能力的可以支持正版! Xshell4 链接: http://pan.baidu.com/s/1jHAgboa 密码: ...
- [转]FastJSON通过SerializeFilter定制序列化
原文地址:https://github.com/alibaba/fastjson/wiki/SerializeFilter 简介 SerializeFilter是通过编程扩展的方式定制序列化.fast ...
- ActiveMQ_日志信息(五)
activemq的日志信息主要配置两个文件 1.conf/log4j.properties 2.conf/logging.properties 来自为知笔记(Wiz)
- 将现有的sql脚本导入 Oracle 数据库,中文乱码问题
将现有的sql 脚本导入 Oracle数据库 比如 在windows 系统下,可以写一个 bat 来实现直接导入 如:bat 中的内容如下,logs.log 将会记录执行日志 sqlplus user ...
- jquery-遍历each
使用案例一 $.ajax({ url : webPath + "/clickCount", type : "POST", dataType : "js ...