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. 图文详解:小白也能看懂的 Kubernetes

    Kubernetes 这个单词来自于希腊语,含义是舵手或领航员 .其词根是 governor 和 cybernetic.K8s 是它的缩写,用 8 字替代了"ubernete". ...

  2. css实现弹框

    CSS遮罩层实现思路:遮罩层的影藏方式一般有display:none.visibility:none.opacity: 0.遮罩层从无到有的出现效果一般是opacity值从0~1,结合transiti ...

  3. R可视化:plot函数基础操作,小白教程

    最近迷恋上了画图,一方面是觉得挺有意思的,另一方面是觉得自己确实画图方面比较弱,所以决定比较系统地回顾反思一下,同时顺带记录下来分享给大家.也确实是好久好久没更新文章了,真的是杂事太多太忙太牵扯精力没 ...

  4. mysql Bad handshake

    由于 Java 程序访问 MySQL 时,MySQL 抛出 Bad handshake 错误,导致接口抛错,然后在 MySQL 配置文件新增 skip_ssl 配置(忽略 SSL 密钥和证书文件),重 ...

  5. 忽略https域名校验不通过

    curl curl 报错: curl: (51) Unable to communicate securely with peer: requested domain name does not ma ...

  6. Android 12(S) 图像显示系统 - GraphicBuffer同步机制 - Fence

    必读: Android 12(S) 图像显示系统 - 开篇 一.前言 前面的文章中讲解Android BufferQueue的机制时,有遇到过Fence,但没有具体讲解.这篇文章,就针对Fence这种 ...

  7. shellcode编写

    shellcode编写 shellcode是一段用于利用软件漏洞而执行的代码,通常使用机器语言编写,其目的往往是让攻击者获得目标机器的命令行shell而得名,其他有类似功能的代码也可以称为shellc ...

  8. node包的降版本

    1.安装版本更高的node包直接到官网去安装. 2.从版本高的node包,降低到版本低的node包. 要先卸载现在的node包,在菜单栏中可以删除. 然后通过https://nodejs.org/zh ...

  9. 【算法】Floyd算法

    什么是Floyd Floyd用于求最短路程.举个栗子,给你一张图,让你求出点[1]到点[5]的最短路程,你会怎么求? (画图工具:CS Academy) 如上图,有向边分别是 1->2  1-& ...

  10. c++ RMQ

    关于 RMQ ,即 Range Maxnum (Minnum) Query .用于查询静态区间最大(最小)值, 思路基于动态规划 (DP) 思路 设 F[i][j] 为 [i,i+2j] 区间内的的最 ...