cf1004.div2.B

https://codeforces.com/contest/2067/problem/B

#include<bits/stdc++.h>
using namespace std;
int t,n;

int x,a[1005],sta[1005],stb[1005];
int main(){
	//freopen("in.txt","r",stdin); 
	//freopen("out.txt","w",stdout); 
	cin>>t;
	while(t--){
		
		cin>>n;
		for(int i=1 ;i<=n;i++){
			cin>>a[i];
			sta[a[i]]++;
		}
		
		for(int i=1;i<=n;i++){
			if(sta[i]>=2){
				sta[i]--;
				stb[i]++;
			}
			if(sta[i]){
				
				sta[i+1]=sta[i+1]+sta[i]-1;
				sta[i]=1;
			}1
		}

		for(int i =1;i<=n;i++){
			if((sta[i]+stb[i])%2!=0)	x=1;
		}	
		
		if(x==1)cout<<"No"<<endl;
		else cout<<"Yes"<<endl;
		 memset(a,0,sizeof(a));
		 memset(sta,0,sizeof(sta));
		 memset(stb,0,sizeof(stb));2
		 x=0;
	}
	return 0;
}

本题主要采用贪心算法,先sta记录每个有价值的物品个数,排序后从最小价值开始操作
先向第二背包转移物品,保证该价值符合条件后向高价值转化,直到剩余物品不够转化(该法由于数据范围较小可以通过),最后判断每个价值下两个背包中的物品是否均分即可(可以优化,因为从最小价值开始操作,即只需记录并判断最后一个价值存在的物品即可,由于本题数据较小便不在优化3

  1. if(sta[i]==0&&sta[i-1]!=0){
    flag = 1;
    break;//i==1e9;
    } ↩︎
  2. 注意每个测试点计算完后要重新初始化数组 ↩︎
  3. 优化可以在1处 ↩︎

评论

  1. 李永泉父亲
    2-12
    2025-2-12 23:49:23

    大石

  2. 李永泉木琴
    2-12
    2025-2-12 23:49:57

    大分

  3. 李家祖宗
    2-12
    2025-2-12 23:52:13

    大变

  4. 博主
    2-13
    2025-2-13 9:21:13

    草拟吗铝热剂

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇