跳到正文

题解

题解:P12648 [KOI 2024 Round 2] 路灯

#SUSPENDED#约 6 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

题目

此题细节太多了,卡了我很久

解题思路

先看样例,首先想到暴力,但 LL 的数据太大了,考虑优化

我们设一个区间的左端点为 ll ,右端点为 rr ,则序列长度为 lenlen

len=r−l−1len = r - l -1 ,则这个序列中最大值为 ⌈len/2⌉ \lceil len /2 \rceil

我们设 answerianswer_i 为黑暗程度为 ii 的位置个数

预估得分 :50pts:50 pts

code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e6+5,M=5e6+5;
int a[N],tot;
unordered_map<int,int> answer;
inline void read(int& x)
{
	x=0;
	char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c))
	{
		x=(x<<3)+(x<<1)+(c^48);
		c=getchar();
	}
}
int stk[15],tp;
inline void write(int x)
{
	if(!x)
	{
		putchar(48);
		putchar('\n');
		return;
	}
	tp=0;
	while(x) stk[++tp]=x%10,x/=10;
	while(tp) putchar(stk[tp--]^48);
	putchar('\n');
}//快读
signed main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	int l,n,k;
	read(l);read(n);read(k);//读入
	for(int i=1;i<=n;i++) read(a[i]);//读入
	//0~a[1]
	answer[0]=n;
	//1~n
	for(int i=1;i<n;i++)
	{
		int len=a[i+1]-a[i]-1;
		int mx;
		if(len==0) continue;
		if(len%2==0) mx=len/2;
		else mx=(len+1)/2;
		if(len&1)
		{
			answer[mx]++;//最中间的加1
			for(int j=mx-1;j>=1;j--) answer[j]+=2;
		}
		else
		{
			for(int j=mx;j>=1;j--) answer[j]+=2;//最中间的加2,分类讨论
		}
	}//处理answer
	int t=0;
	while(1)
	{
		if(tot<=l-a[n]&&tot!=0) answer[tot]++;//n~L的区间
		if(tot<=a[1]&&tot!=0) answer[tot]++;//0~a[1]的区间
		int t2=t;
		t+=answer[tot];
		if(t<k)
		{
			for(int i=1;i<=answer[tot];i++)
			{
				write(tot);
			}
		}
		else
		{
			for(int i=1;i<=k-t2;i++)
			{
				write(tot);
			}
            return 0;
		}
		tot++;
	}//输出
	return 0;
}

50分了,但还会 MLE+TLEMLE+TLE,重读一遍代码,发现 answer[0]=n;answer[0]=n;

answer[mx]++;answer[mx]++;

注意到了什么 ::

这不是差分吗 ??????

即可用差分优化掉一层循环

预估得分 :100pts:100pts

code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e6+5,M=5e6+5;
int a[N],tot;
unordered_map<int,int> answer;
inline void read(int& x)
{
	x=0;
	char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c))
	{
		x=(x<<3)+(x<<1)+(c^48);
		c=getchar();
	}
}
int stk[15],tp;
inline void write(int x)
{
	if(!x)
	{
		putchar(48);
		putchar('\n');
		return;
	}
	tp=0;
	while(x) stk[++tp]=x%10,x/=10;
	while(tp) putchar(stk[tp--]^48);
	putchar('\n');
}//快读
signed main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	int l,n,k;
	read(l);read(n);read(k);//读入
	for(int i=1;i<=n;i++) read(a[i]);//读入
	//0~a[1]
//	answer[0]=n;
	answer[0]=n;
	answer[1]=-n;//差分
	//1~n
	for(int i=1;i<n;i++)
	{
		int len=a[i+1]-a[i]-1;
		int mx;
		if(len==0) continue;
		if(len%2==0) mx=len/2;
		else mx=(len+1)/2;
		if(len&1)
		{
//			answer[mx]++;
			answer[1]+=2;
			answer[mx]--;
			answer[mx+1]--;//差分
		}
		else
		{
			answer[1]+=2;
			answer[mx+1]-=2;//差分
		}
	}
	int t=0,ans=0;
	while(1)
	{
		if(tot<=l-a[n]&&tot!=0) answer[tot]++,answer[tot+1]--;
		if(tot<=a[1]&&tot!=0) answer[tot]++,answer[tot+1]--;
		ans+=answer[tot];//求前缀和
		int t2=t;
		t+=ans;
		if(t<k)
		{
			for(int i=1;i<=ans;i++)
			{
				write(tot);
			}
		}
		else
		{
			for(int i=1;i<=k-t2;i++)
			{
				write(tot);
			}
            return 0;
		}
		tot++;
	}//输出
	return 0;
}

时间复杂度 :O(k):O(k)

AC

警告

十年OI一场空 ,, 不开_______见祖宗

原文