题解:P12648 [KOI 2024 Round 2] 路灯
ZackofZHOU · · 题解
思路
解析
寻找的
方法
枚举每一个
警示
- 十年 OI 一场空,不开
long long见祖宗。 -
AC 记录
完整代码:(有注释)
#include<unordered_map>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll; // 十年OI一场空,不开long long见祖宗。
const int N = 3e5 + 10;
int n,k,cnt;
ll L,a[N];
unordered_map <ll,bool> vis; // 标记
void Make(ll x,ll y) // 判断输出
{
ll z = x + y;
if(0 <= z && z <= L) // 是否在范围之内
{
if(vis.count(z))
return;
vis[z] = true;
cout << abs(y) << '\n'; // 输出
cnt++;
if(cnt == k)
exit(0);
}
}
void Work(ll x)
{
for(int i = 1;i <= n;i++) // 枚举每一个a_i
{
Make(a[i],x);
Make(a[i],-x);
}
Work(x + 1); // 递归,仍然是枚举
}
int main()
{
cin >> L >> n >> k;
for(int i = 1;i <= n;i++)
cin >> a[i];
Work(0);
return 0;
}