To CF

这道题是排序贪心,将原序列排序后统计答案即可。

但是直接统计会超时,因为排序后具有单调性,所以可以进行一点优化,这样,便可以通过此题。

而这道题的优化在于单调性,因为 \(a[i+1]\) 必然大于 \(a[i]\),所以当 \(a[j]\) 无法与 \(a[i]\) 匹配时,也就可以排除掉 \(a[i+1]\),原因是因为具有单调性。

因此,不需要重新开始匹配,直接向下匹配即可。

这样,也就可以通过本题,避免超时。

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n;//共n个数
int s[100005];
int ans;//统计答案
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&s[i]);
}
sort(s,s+n);//排序
int j=0;
for(int i=0;i<n-1&&j<n;i++){//统计答案,优化:可以继续从s[j]进行统计
j++;
if(s[j]>s[i]) ans++;
else i--;
}
printf("%d",ans);//输出答案
return 0;
}

CF1007A Reorder the Array 题解的更多相关文章

  1. CF 1008C Reorder the Array

    You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it ...

  2. [LeetCode]Remove Duplicates from Sorted Array题解

    Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...

  3. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

  4. A. Reorder the Array

    You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it ...

  5. Codeforces Round #497 (Div. 2) C. Reorder the Array

    Bryce1010模板 http://codeforces.com/contest/1008/problems #include <bits/stdc++.h> using namespa ...

  6. CodeForces-1007A Reorder the Array 贪心 田忌赛马

    题目链接:https://cn.vjudge.net/problem/CodeForces-1007A 题意 给个数组,元素的位置可以任意调换 问调换后的元素比此位置上的原元素大的元素个数最大多少 思 ...

  7. CF83A Magical Array 题解

    Content 有一个长度为 \(n\) 的序列 \(a_1,a_2,a_3,...,a_n\).定义一个"神奇数组"为在上面的序列中最大值和最小值相等的子序列.求出这个序列中&q ...

  8. CF1514A Perfectly Imperfect Array 题解

    Content 给定一个长度为 \(n\) 的序列,问是否存在一个非空子序列,使得这个子序列所有元素的积不是完全平方数. 数据范围:\(t\) 组数据,\(1\leqslant t\leqslant ...

  9. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

随机推荐

  1. Python自定义排序及我实际遇到的一些题目实例

    写在前面,本文主要介绍Python基础排序和自定义排序的一些规则,如果都比较熟悉,可以直接翻到第三节,看下实际的笔试面试题中关于自定义排序的应用. 一.基础排序 排序是比较基础的算法,与很多语言一样, ...

  2. MySQL中的全表扫描和索引树扫描

    引言 在学习mysql时,我们经常会使用explain来查看sql查询的索引等优化手段的使用情况.在使用explain时,我们可以观察到,explain的输出有一个很关键的列,它就是type属性,ty ...

  3. Vue-router(前端路由)的两种路由模式

    Vue的两种路由模式: hash.history:默认是hash模式: 前端路由(改变视图的同时不会向后端发出请求) 一.什么是hash模式和history模式? hash模式:是指url尾巴后的#号 ...

  4. Linux Troubleshooting 超实用系列 - Disk Analysis

    笔者历史文章: https://github.com/CarlJi/words 关于磁盘的使用,实际生产中以下问题会较为常见: No space left on device - 空间不足 Disk ...

  5. 免费yum源镜像地址

    收集的镜像,yum源等网站地址 阿里巴巴开源镜像站 https://opsx.alibaba.com/mirror http://mirrors.aliyun.com/centos/ 网易开源镜像站 ...

  6. Centos7部署Redis集群

    Redis简介 Redis(Remote Dictionary Server)是完全开源的.遵守BSD协议的.高性能的Key-Value数据库. Redis与其他Key-Value缓存产品有一下三个特 ...

  7. Fail2ban 安装Fail2ban到Ubuntu

    系统版本:Ubuntu 16.04.5 LTS 软件版本:fail2ban-0.9.3 硬件要求:无 1.安装Fail2ban root@local:~# apt-get update root@lo ...

  8. springcloud-- Alibaba-nacos--支持的几种服务消费方式

    通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...

  9. (数据科学学习手札137)orjson:Python中最好用的json库

    本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,我们在日常使用Pytho ...

  10. np.linspace,numpy中的linspace()

    import numpy as np x=np.linspace(1,10) y=np.linspace(1,10,num=10,retstep=True)#num可省略 print(x) print ...