看到题目的时候,以为类似插入排序,比较第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. EasyUi–7.tab和datagrid和iframe的问题

    1. 多个tab切换,第2个不显示 动态添加tab Iframe页面的方法 展开 折叠 <script type="text/javascript"> $(functi ...

  2. Linux Shell 高级编程技巧3----运行级别脚本介绍

    3.运行级别脚本介绍    3.1.运行级别        运行级别介绍:            0    关机            1    单用户模式            2    多用户模式 ...

  3. Validform 学习笔记---代码练习

    上一节主要梳理了validform的基础知识,针对这些基础知识,编写代码的时候,也整理的部分知识,先记录以便后期温习. 验证部分的css @charset "utf-8"; /* ...

  4. 动态生成SQL执行语句

    SET @qry = 'SELECT product_cd, name, product_type_cd, date_offered, date_retired FROM product WHERE ...

  5. [JavaCore] 取得类的字节码、取得类的装载器

    三种方式取得类的字节码: 1. 类名.class BranchInfoService.class 2. 对象名.getClass() branchInfoService.getClass() 3. C ...

  6. jquery audio player

    <!DOCTYPE html><html lang="en"> <head>  <meta charset="utf-8&quo ...

  7. BZOJ 3289: Mato的文件管理 莫队+BIT

    3289: Mato的文件管理 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的 ...

  8. hdu 3001(状压dp, 3进制)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 由于本题中一个点最多能够访问2次,由此可以联想到3进制; visited[i][j]表示在状态i ...

  9. cf #365b 巧妙的统计

     Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  10. 移动Web开发规范

    1.字体设置 使用无衬线字体 body { font-family: "Helvetica Neue", Helvetica, STHeiTi, sans-serif; } 2.设 ...