CF1007A Reorder the Array 题解
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 题解的更多相关文章
- CF 1008C Reorder the Array
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it ...
- [LeetCode]Remove Duplicates from Sorted Array题解
Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- A. Reorder the Array
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it ...
- Codeforces Round #497 (Div. 2) C. Reorder the Array
Bryce1010模板 http://codeforces.com/contest/1008/problems #include <bits/stdc++.h> using namespa ...
- CodeForces-1007A Reorder the Array 贪心 田忌赛马
题目链接:https://cn.vjudge.net/problem/CodeForces-1007A 题意 给个数组,元素的位置可以任意调换 问调换后的元素比此位置上的原元素大的元素个数最大多少 思 ...
- CF83A Magical Array 题解
Content 有一个长度为 \(n\) 的序列 \(a_1,a_2,a_3,...,a_n\).定义一个"神奇数组"为在上面的序列中最大值和最小值相等的子序列.求出这个序列中&q ...
- CF1514A Perfectly Imperfect Array 题解
Content 给定一个长度为 \(n\) 的序列,问是否存在一个非空子序列,使得这个子序列所有元素的积不是完全平方数. 数据范围:\(t\) 组数据,\(1\leqslant t\leqslant ...
- LeetCode Find Minimum in Rotated Sorted Array
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...
随机推荐
- 文件传输协议:FTP、TFTP、SFTP有什么区别?
一个执着于技术的公众号 FTP 提供一种在服务器和客户机之间上传和下载文件的有效方式:是基于TCP的传输,FTP采用双TCP连接方式:支持授权与认证机制,提供目录列表功能. ---控制连接使用TCP端 ...
- 0.1+0.2不等于0.3,微信小程序云开发如何解决JavaScript小数计算精度失准的问题
先看图 这个是JavaScript语言自身存在的一个问题.说道这里不得不提一下网上流传的JavaScript搞笑图 我们在使用云开发来开发微信小程序的时候,会经常遇到JavaScript小数计算精度失 ...
- 1903021121-刘明伟 实验一 19信计JAVA—Markdown排版学习
项目 内容 班级博客链接 19信计班(本) 作业要求链接 实验一 课程学习目标 学习使用Markdown排版 这个作业帮助我们实现了什么学习目标 学会使用Markdown排版 任务一:在博客园平台注册 ...
- 好客租房4-react的基本使用 方法说明
2.2方法说明 React.createElement //第二步创建react元素 //参数1:元素名称 //参数2:元素属性 //参数3:元素的子节 ...
- .net 获取IP地址的几种方式
1.获取服务器IP地址: 1) Local_Addr var Local_Addr = Request.ServerVariables.Get("Local_Addr").ToSt ...
- 【动态规划】统计蚂蚁 (ants)
题目 描述 蚂蚁山上有T(1<=T<=1,000)种蚂蚁,标记为1..T,每种蚂蚁有N_i只蚂蚁(1<=N_i<=100),现有A(A<=5000)只蚂蚁,从中选出S,S ...
- Linux如何安装JDK1.8版本详细步骤
Linux如何安装JDK1.8版本详细步骤 1.下载JDK1.8版本压缩包 进入官网:https://www.oracle.com/java/technologies/downloads/ 2.将压缩 ...
- Ribbon的ServerStats引起内存泄露问题总结
问题描述 服务运行一段时间之后,出现页面卡顿加载慢的问题,使用top命令查看了服务器的使用情况,发现CPU飙高,接着查看了该进程中每个线程的占用情况,发现导致CPU高的线程是JVM垃圾回收的线程,然后 ...
- springboot执行流程
构造方法初始化,创建一个新的实例,这个应用程序的上下文要从指定的来源加载bean public SpringApplication(ResourceLoaderresourceLoader,Class ...
- pytorch初学
(pytorch_gpu) D:\pytorch-text>pythonPython 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 ...