跳到正文

题解

题解:CF2102B The Picky Cat

Technocy约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年5月15日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 3 分钟
获赞
2
题目
CF2102B

版本

  1. v1当前

题意

给出一个数 nn 与数组 ana_n,保证对于任意 1≤i<j≤n1\le i<j\le n,∣ai∣≠∣aj∣|a_i|\ne|a_j|,问是否可以通过任意次 ai×−1a_i\times -1 使 a1a_1 成为中位数。

思路

在输入数组时每一项都取其绝对值,然后进行从小到大的排序,令 cntcnt 为绝对值小于 ∣a1∣|a_1| 的元素的数量,cnt1cnt1 为绝对值大于 ∣a1∣|a_1| 的元素的数量,当 cnt≤⌊n2⌋cnt\le\lfloor\frac{n}{2}\rfloor 时,输出 YES,否则为 NO。

解释

当 cnt≤⌊n2⌋cnt\le\lfloor\frac{n}{2}\rfloor 时,cnt1≥⌊n2⌋−cntcnt1\ge\lfloor\frac{n}{2}\rfloor-cnt,那么此时可以使 ⌊n2⌋−cnt\lfloor\frac{n}{2}\rfloor-cnt 个绝对值比 ∣a1∣|a_1| 大的数取负来使 a1a_1 成为中位数,故输出 YES。

当 cnt>⌊n2⌋cnt>\lfloor\frac{n}{2}\rfloor 时,绝对值比 ∣a1∣|a_1| 大的数不能取负,若取负会导致 a1a_1 的大小排名远离 ⌈n2⌉\lceil\frac{n}{2}\rceil,若由绝对值比 ∣a1∣|a_1| 小的数取负 a1a_1 的大小排名则不会变化,故输出 NO。

Code

C++
#include<bits/stdc++.h>
#define N 100086
#define ll long long
#define iakioi cout<<"I AK IOI"<<endl
using namespace std;
struct node{
	int snum,id;						//id表示原数组中第几个输入的数,方便找到a[1],snum则为其绝对值 
}a[N];
int t,n;
inline int read();
inline bool cmp(node x,node y);	 
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);

	t=read();
	while(t--){
		n=read();
		int mid=n/2;
		for(int i=1;i<=n;i++){
			a[i].snum=abs(read());
			a[i].id=i;
		}
		sort(a+1,a+1+n,cmp);			//按绝对值从小到大排序 
		int cnt=0;
		for(int i=1;i<=n;i++){
			if(a[i].id==1) break;		//寻找绝对值小于a[1]的绝对值的数的个数 
			cnt++;
		}
		if(cnt<=mid) cout<<"YES"<<endl;	//判断 
		else cout<<"NO"<<endl;
	}
	return 0;
}
inline int read(){
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9') {x=x*10+ch-48;ch=getchar();}
	return x*f;
}
inline bool cmp(node x,node y){
	return x.snum<y.snum;
}
原文