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. [ Module ] 环境变量管理工具 Module 安装和使用

    https://www.cnblogs.com/yeungchie/ 1. 工具下载 手动下载 modules-5.1.0 点击下载 wget 下载 wget https://jaist.dl.sou ...

  2. C\C++白嫖学习

    一.C语言的学习     推荐B站的郝斌老师的C语言教程,播放量可观.注意在学习前可在评论区"找宝藏". 大家可能也知道B站的小甲鱼,个人感觉内容不够优质,讲得有点粗糙,讲的故事有 ...

  3. 判断数据类型(typeof&instanceof&toString)

    一.数据类型 ES6规范中有7种数据类型,分别是基本类型和引用类型两大类 基本类型(简单类型.原始类型):String.Number.Boolean.Null.Undefined.Symbol 引用类 ...

  4. python之re模块补充和其他模块(collection、time、queue、datetime、random)

    目录 re模块补充说明 collections模块 queue模块 time模块 datetime模块 random模块 re模块补充说明 在正则表达式中,'()'的作用是进行分组,但是在re模块中, ...

  5. git实战-多分支开发-2022新项目

    现在开发中大多数公司中都在使用Git这个代码版本管理工具,几乎可以说是已经成为标配,刚入职不久的这家新公司也不例外. 去公司没多久,开始搭建项目,然后创建开发分支,有多少个后端人员就创建多少个开发分支 ...

  6. 12.MYSQL基础-常见函数

    4. 常见函数 一.字符函数 概念 类似于Java的方法,将一组逻辑语句封装在方法中,对外暴露方法名 优点 隐藏了实现细节 提高代码的重用性 调用 select 函数名(实参列表) [ from 表] ...

  7. 《Unix 网络编程》13:守护进程和 inetd 超级服务器

    守护进程和 inetd 超级服务器 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ...

  8. MVC - Request对象的主要方法

    MVC - Request对象的主要方法 setAttribute(String name,Object):设置名字为name的request的参数值 getAttribute(String name ...

  9. k8s中label和label selector的基本概念及使用方法

    1.概述 在k8s中,有一个非常核心的概念,就是label(标签),以及对label的使用,label selector. 本文档中,我们就来看看:1.什么是标签,2.如何定义标签,3.什么是标签选择 ...

  10. 【实操干货】做好这 16 项优化,你的 Linux 操作系统焕然一新

    大家好,这次跟大家谈谈又拍云的操作系统优化方案.往简单地说,我们使用的 Linux 操作系统主要都是基于 CentOS6/7 的精简和优化.往复杂地说,则是我们有两套系统,业务上使用的定制 Linux ...