看到题目的时候,以为类似插入排序,比较第i个元素和第i-1个元素,

如果第i个元素比第i-1个元素小,则不交换

如果第i个元素比第i-1个元素大,则交换第i个元素和第i-1个元素

  继续比较第i-1个元素与前一个元素,直到前一个元素大为止

交换元素次大于等于k则停止

但对测试用例

1234 3

则出现问题,如果按照上述方法得到答案为3214,

但正确答案是4321,直接将第4个元素往前交换

故在上面基础上改进得

当扫描第i个元素时,则要取出[i,i+k+1)之间的最大元素,然后将最大元素往前交换,则交换后第i个元素肯定是最大的

#include <iostream>
#include <vector>
#include <string>
#include <algorithm> using namespace std; int main(){
string s;
int k;
cin >> s >> k;
int len = s.length();
for(int i = 0 ; i < len; ++ i){
int maxIndex = i;
for(int j = i +1; j < min(len,i+k+1); ++ j){
if(s[maxIndex] < s[j]) maxIndex = j;
}
for(int j = maxIndex; j > i; --j){
swap(s[j],s[j-1]);
k--;
}
}
cout<<s<<endl;
}

  

Codeforces Round #249 (Div. 2) B. Pasha Maximizes的更多相关文章

  1. 模拟 Codeforces Round #249 (Div. 2) C. Cardiogram

    题目地址:http://codeforces.com/contest/435/problem/C /* 题意:给一组公式,一组数据,计算得到一系列的坐标点,画出折线图:) 模拟题:蛮恶心的,不过也简单 ...

  2. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  3. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

  4. Codeforces Round #249 (Div. 2)B(贪心法)

    B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #337 (Div. 2) A. Pasha and Stick 数学

    A. Pasha and Stick 题目连接: http://www.codeforces.com/contest/610/problem/A Description Pasha has a woo ...

  6. Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题

    B. Pasha and Tea Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/prob ...

  7. Codeforces Round #330 (Div. 2) B. Pasha and Phone 容斥定理

    B. Pasha and Phone Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/595/pr ...

  8. Codeforces Round #326 (Div. 2) B. Pasha and Phone C. Duff and Weight Lifting

    B. Pasha and PhonePasha has recently bought a new phone jPager and started adding his friends' phone ...

  9. Codeforces Round #249 (Div. 2) C题,模拟画图 ----未解决!

    http://codeforces.com/contest/435/problem/C

随机推荐

  1. Java中读取xml方法

    package zaxiang; import java.io.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parser ...

  2. Android 快速开发框架:推荐10个框架:afinal、ThinkAndroid、andBase、KJFrameForAndroid、SmartAndroid、dhroid..

    对于Android初学者以及对于我们菜鸟,这些大神们开发的轻量级框架非常有用(更别说开源的了). 下面转载这10个框架的介绍:(按顺序来吧没有什么排名). 一.  Afinal 官方介绍: Afina ...

  3. mysql基础一

    一.概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serve ...

  4. react.js基本操练

    慢慢了解其它的JS前端框架... var data = [{ "when": "2 minutes ago", "who": "J ...

  5. Howto: Connect MySQL server using C program API under Linux or UNIX

    From my mailbag: How do I write a C program to connect MySQL database server? MySQL database does su ...

  6. 在Window的IIS中创建FTP的Site并用C#进行文件的上传下载

    文件传输协议 (FTP) 是一个标准协议,可用来通过 Internet 将文件从一台计算机移到另一台计算机. 这些文件存储在运行 FTP 服务器软件的服务器计算机上. 然后,远程计算机可以使用 FTP ...

  7. HDU 4005 The war Tarjan+dp

    The war Problem Description   In the war, the intelligence about the enemy is very important. Now, o ...

  8. POJ 2114 Boatherds 树分治

    Boatherds     Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...

  9. WPF标准控件模板查看程序(文件里面)

    xaml <Window x:Class="ControlTemplateBrowser.MainWindow" xmlns="http://schemas.mic ...

  10. ZOJ 3494 BCD Code(AC自动机+数位DP)

    BCD Code Time Limit: 5 Seconds      Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...