跳到正文

题解

P13713 晴天题解

Cosmos_zzx约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年8月15日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 4 分钟
获赞
2
题目
P13713

版本

  1. v1当前

思路

尝试构造答案。

我们发现,1 1 -1 -1里有 11 个晴天,11 个阴天,而更简单的构造是这样的:1 0 0 -1,虽然这两种构造所产生的晴阴天数量是一样的,但是明显,第二种构造中要考虑的东西更少,更好考虑,所以我们用第二种构造解决此问题。

可以按照这样的顺序构造答案序列:

代码
         这个分隔符很重要
                ↓
1 0 1 0 ... 1 0 0 0 0 ... 0 0 -1 0 -1 0 ... -1 0
|_______________| |_________| |________________|
晴天的(结尾时要分类讨论)中间用0分隔 阴天的(结尾时分类讨论)

那为什么晴天与阴天序列都要在结尾分类讨论呢?

先看晴天:

代码
如果晴天天数为偶数(假如为4):
1 0 1
按照奇偶性先写出4-1=3个数,接着再在它结尾写上一个1和0,这是最短的方案,如下:
1 0 1 1 0|0 0 0 0 ...
  ^ ^ ^ ^
让前面最短,给后面腾出空间显然是最优的。

如果晴天天数为奇数呢(假设为5)?
1 0 1 0 1|0 0 0
  ^ ^ ^ ^ ^ 
此时有5个晴天,满足条件,可是最后一个晴天到了要补零的地方,为了避免混淆,我们手动将那个0调到晴天的地盘上便可。

阴天也是一样的分类讨论(可以手推一下),所以就到了代码时间(注意有两个特例需要特判:一个是1 0 0,要输出 11 个 00, 另一个是2 0 0,要输出 22 个 00):

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
#define getchar_unlocked() getchar()
inline int read() {int x=0,ff=1;char ch=getchar_unlocked();while(ch<48||ch>57) {if(ch==45)ff=-1;ch=getchar_unlocked();}while(ch>=48&&ch<=57) x=(x<<3)+(x<<1)+(ch^48),ch=getchar_unlocked();return x*ff;}
inline void write(int x,char ch){if(!x){putchar(48),putchar(ch);return;}if(x<0)putchar(45),x=-x;char a[20];int i=0;while(x)a[++i]=x%10+48,x/=10;for(;i;i--)putchar(a[i]);putchar(ch);}
const int N=1e6+114;
int T;
int x,y,n,a[N];
signed mian()
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--)
	{
		cin>>n>>x>>y;
		if(n==1 && x==0 && y==0)cout<<0<<endl;
		else if(n==2 && x==0 && y==0)cout<<0<<' '<<0<<endl;
		else if(x+y>n-2)cout<<-2<<endl;
		else
		{
			vector<int> v,v2;
			int sumx=0,sumy=0;
			if(x%2)
			{
				for(int i=1;i<=x;i++)v.push_back(i%2);
				v.push_back(0);
			    sumx=x+1;
			}
			else if(x!=0)
			{
				for(int i=1;i<=x-1;i++)v.push_back(i%2);
				v.push_back(1),v.push_back(0);
				sumx=x+1;
			}
			if(y%2)
			{
				for(int i=1;i<=y;i++)v2.push_back((i%2?0:-1));
				v2.push_back(-1);
				sumy=y+1;
			}
			else if(y!=0)
			{
				for(int i=1;i<=y;i++)v2.push_back((i%2?0:-1));
				v2.push_back(0);
				sumy=y+1;
			}
			for(auto it:v)cout<<it<<' ';
			for(int i=1;i<=n-sumx-sumy;i++)cout<<0<<' ';
			for(auto it:v2)cout<<it<<' ';
			cout<<endl;
		}
	}
	return 0;
}
/*
1
5
2 4 2 5 2
1 3 2 5 4
*/
不要抄代码!