题目链接:https://cn.vjudge.net/problem/CodeForces-1007A

题意

给个数组,元素的位置可以任意调换

问调换后的元素比此位置上的原元素大的元素个数最大多少

思路

一开始想了半天,最后想出来田忌赛马

田忌赛马经典题,一共5种可能性,详见HDU-1052 Tian Ji -- The Horse Racing 贪心 考虑特殊位置(首尾元素)的讨论

提交过程

AC

代码

#include <cstdio>
#include <algorithm>
using namespace std;
int main(void){
int n, a[100000+20], b[100000+20]; scanf("%d", &n);
for (int i=0; i<n; i++) scanf("%d", &a[i]), b[i]=a[i];
sort(a, a+n); sort(b, b+n); int alast=n-1, head=0, last=n-1, i=0, cnt=0;
while (i<=alast){
if (b[i]<a[head]){
head++; cnt++; i++;
}else if (b[i]>a[head]){
head++; alast--;
}else if (b[i]==a[head]){
if (b[alast]<a[last]){
alast--; last--; cnt++;
}else if (b[alast]>a[last]){
alast--; head++;
}else{
if (a[head]<b[alast]);
else if (a[head]>b[alast]) cnt++;
alast--; head++;
}
}
}printf("%d\n", cnt); return 0;
}
Time Memory Length Lang Submitted
62ms 604kB 812 GNU G++ 5.1.0 2018-07-23 19:41:44

CodeForces-1007A Reorder the Array 贪心 田忌赛马的更多相关文章

  1. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

  2. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  3. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  4. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  5. Codeforces 754A Lesha and array splitting(简单贪心)

    A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...

  6. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  7. Codeforces Round #258 (Div. 2) . Sort the Array 贪心

    B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...

  8. CodeForces ---596B--Wilbur and Array(贪心模拟)

    Wilbur and Array Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Su ...

  9. codeforces 442C C. Artem and Array(贪心)

    题目链接: C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. Thingworx SDK开发自定义Widget

    Thingworx自带的图表数量有限,样式也很有限,在echarts上看到了这样一个非常简单的图表,下面将做一个简单的静态引入示范 首先创建Thingworx项目 然后右键ui新建widget 自动生 ...

  2. RxSwift 之变换操作

    https://www.aliyun.com/jiaocheng/349821.html RxSwift入坑解读-你所需要知道的各种概念 http://www.open-open.com/lib/vi ...

  3. Qwiklab'实验-DynamoDB, Redshift, Elasticsearch'

    title: AWS之Qwiklab subtitle: 4. Qwiklab'实验-Amazon DynamoDB, Amazon Redshift, Elasticsearch Service' ...

  4. ajax第二天学习

    post方式发送请求 要首先设置请求头(参数设置为ajax.setRequestHeader("content-type","application/x-www-form ...

  5. Laravel 5.5 项目安装报错

    错误一. [Composer\Exception\NoSslException] The openssl extension is required for SSL/TLS protection bu ...

  6. es6——map-set与对象对比

    {    //map,set,object对比    let item={t:1};    let map=new Map();    let set=new Set();    let obj={} ...

  7. 论文阅读《End-to-End Learning of Geometry and Context for Deep Stereo Regression》

    端到端学习几何和背景的深度立体回归 摘要     本文提出一种新型的深度学习网络,用于从一对矫正过的立体图像回归得到其对应的视差图.我们利用问题(对象)的几何知识,形成一个使用深度特征表示的代价量(c ...

  8. vue实现双向绑定原理

  9. SpringBoot中使用过滤器Filter

    场景:API的参数都是经过加密的,于是在过滤器中,将获取到的请求的参数先解密再去进行处理 一.实现Filter接口 public class TestFilter implements Filter ...

  10. ASP.NET-表单验证-DataAnnotations

    DataAnnotations  [数据注解,数据注释] 需要引入两个脚本文件 <script src="@Url.Content("~/Scripts/jquery.val ...