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. go convert slice to struct

    Question: in golang how to convert slice to struct scene 1:use reflect convert slice to struct func ...

  2. linux篇-centos7安装samba服务器

    1查看是否安装samba服务 2如果为空则没有安装,安装显示安装完成即成功 3查看samba状态 4查看配置文件的位置 5配置文件备份,直接传输到本地备份 6修改配置文件 Path共享目录位置 Val ...

  3. linux篇-linux mysql数据库定时备份

    1在linux上面创建一个文件夹,并且进行备份 cd /home mkdir backup cd backup 2创建一个脚本 Vi imaginebase.sh #!/bin/bash mysqld ...

  4. 『忘了再学』Shell基础 — 26、cut列提取命令

    目录 1.cut命令说明 2.cut命令练习 (1)cut命令基本用法 (2)cut命令选取多列 (3)按字符来进行提取 (4)按指定分隔符进行截取数据 3.cut命令分隔符说明 1.cut命令说明 ...

  5. python生产exe文件yi以及解释器配置等

    原文链接:https://blog.csdn.net/weixin_42691768/article/details/81044666 https://www.cnblogs.com/paulwhw/ ...

  6. Linux系统安装ActiveMQ

    下载安装包 https://activemq.apache.org/components/classic/download/ 上传至服务器并解压 [root@localhost activemq]# ...

  7. Apache Shiro反序列化漏洞(Shiro550)

    1.漏洞原理: Shiro 是 Java 的一个安全框架,执行身份验证.授权.密码.会话管理 shiro默认使用了CookieRememberMeManager,其处理cookie的流程是:得到rem ...

  8. SAP Column tree

    code as bellow *&---------------------------------------------------------------------* *& I ...

  9. UiPath官方视频Level1

    [UiPath官方视频Level1]第一课-UiPath简介https://www.bilibili.com/video/BV1zJ41187vB [UiPath官方视频Level1]第二课-变量和数 ...

  10. python常见的错误提示处理

    python常见的错误有 NameError变量名错误 IndentationError代码缩进错误 AttributeError对象属性错误 TypeError类型错误 IOError输入输出错误 ...