Orz 想出题解的神犇。。。
其实思想挺简单,就是保证没有连续的 2 即可,分两种情况:
假设当前加上
(1)
找到
p
之后的第一个
这样最大单词操作次数 ≤3 ,满足题意。
Ac Code:
#include <cstdio>
#include <set>
using namespace std;
const int MAXN = 1009;
const int INF = 1e9;
int n, m;
int v[MAXN];
set<int> zero, two;
set<int>::iterator t0, t2;
void update(int p, int add)
{
if(v[p] == 0) zero.erase(p);
if(v[p] == 2) two.erase(p);
v[p] += add;
printf(" %d %d", p, v[p]);
if(v[p] == 0) zero.insert(p);
if(v[p] == 2) two.insert(p);
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 0; i <= n; ++i)
zero.insert(i);
two.insert(n);
while(m--)
{
int p;
scanf("%d", &p);
t2 = two.upper_bound(p);
t0 = zero.upper_bound(*t2);
if(p >= *(--t0))
{
printf("3");
update(p, 1);
update(*t2, -2);
update(*t2+1, 1);
}
else if(v[p] > 0)
{
printf("2");
update(p, -1);
update(p+1, 1);
}
else
{
printf("1");
update(p, 1);
}
puts("");
}
return 0;
}