题目链接: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. maven的pom.xml配置json依赖

    <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</art ...

  2. 页面元素的定位:getBoundingClientRect()和document.documentElement.scrollTop

    1.document.documentElement.getBoundingClientRect MSDN对此的解释是: Syntax oRect = object.getBoundingClient ...

  3. zabbbix4.0升级到4.2

    一.添加Zabbix存储库 1.安装存储库配置包. rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4 ...

  4. Numpy的使用规则

    之前安装的python版本是3.7 各种库都是自己一个一个下载安装的 很操心 各种缺功能 后来发现了anaconda 啊 真是一个好东西 简单来说 它就是一个涵盖大部分常用库的python包 一次安装 ...

  5. python 进程 multiprocessing模块

    一.multiprocess.process模块 1.process类 Process([group [, target [, name [, args [, kwargs]]]]]),由该类实例化得 ...

  6. react中的跨域问题

    react中的跨域问题

  7. Spring IOC过程

    https://www.processon.com/diagraming/5c96171fe4b0f88919b98497 1. AbstractApplicationContext:执行refres ...

  8. nutch+hadoop 配置使用

    nutch+hadoop 配置使用 配置nutch+hadoop 1,下载nutch.如果不需要特别开发hadoop,则不需要下载hadoop.因为nutch里面带了hadoop core包以及相关配 ...

  9. SQL SERVER-集合操作

    内连接      INNER JOIN(等值连接):只显示两个表中联结字段相等的行.这个和用select查询多表是一样的效果,所以很少用到:外连接:LEFT JOIN :以左表为基础,显示左表中的所有 ...

  10. 极路由4pro安装java(Jamvm 2.0.0 + gnu classpath 0.9.8)

    首先试了gnu classpath 0.9.9,编译不过后来改成0.9.8 编译环境 OS: 64位 Ubuntu 16.04 LTS(vmware虚拟机) SDK: 用之前讲过的官方SDKmtmip ...