跳到正文

题解

UVA13180

Sazanami约 3 分钟
原文

这是 2025年12月6日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
UVA13180

版本

  1. v2当前
  2. v1

题意

给定一个序列,判断它是否可以在排列之后使得按中心向左右依次严格递减,并且左右对称。如果可以输出排序后的序列。

做法

  • 如果长度是偶数,显然不行;
  • 如果最大的数不止有一个,或者不是最大的不是有两个,都不行。

判断完这些之后,我们就可以输出可构造出的满足题目要求的序列了。

代码

C++
#include<bits/stdc++.h> 
using namespace std;
const int N=1005; 
int main(){
	long long p[N];
	int cnt=0;
	long long x; //多测 
	while(cin>>x){
		if(x==0){
			if(cnt==0) break;
			//偶数明显不行 
			if(cnt%2==0){
				cout<<"NO\n";
				cnt=0;
				continue;
			}
			sort(p,p+cnt);
			long long maxn=p[cnt-1];
			int maxc=0;
			for(int i=0;i<cnt;++i){
				if(p[i]==maxn) maxc++;
			}
			if(maxc!=1){ //如果最大的个数不止有一个 
				cout<<"NO\n";
				cnt=0;
				continue;
			}
			map<long long,int> m;
			for(int i=0;i<cnt;++i) m[p[i]]++;
			bool flag=1;
			for(auto it=m.begin();it!=m.end();++it){
				if(it->first==maxn) continue;
				if(it->second!=2){
					flag=0;
					break;
				}
			}
			if(!flag){ //除了最大的以外,每个必须存在两个相同的 
				cout<<"NO\n";
				cnt=0;
				continue;
			}
			set<long long> s; //set去重 
			for(int i=0;i<cnt;++i){
				if(p[i]!=maxn) s.insert(p[i]);
			}
			long long L[N];
			int l=0;
			//输出 
			for(auto it=s.begin();it!=s.end();++it) 
				L[l++]=*it;
			for(int i=0;i<l;++i){
				if(i>0) cout<<" ";
				cout<<L[i];
			}
			if(l>0) cout<<" ";
			cout<<maxn;
			for(int i=l-1;i>=0;--i) cout<<" "<<L[i];
			cout<<"\n";
			cnt=0;
		}else{
			if(cnt<N) p[cnt++]=x;
		}
	}
	return 0;
}

记录