https://codeforces.com/problemset/problem/1428/D
思路:
从前往后会发现前面的状态会影响后面的情况。从后往前构造。
而且此时从后往前构造可以发现:
1、弹射两次的飞镖,肯定需要右边弹射一次飞镖的列作为最后一次弹射。
2、弹射三次的飞镖既可以利用右边弹射一次的列又可以利用右边弹射两次的列。
因此再结合从下往上构造。
然后就再分三种情况考虑:
对于弹射一次的列,我们每次都放到(i,i),即放在主对角线上,然后将该点的行列坐标存入que1。
对于弹射两次的列,从que1中取出队首的坐标关系,然后在同行放障碍,然后将该列的坐标关系存入que2。
对于弹射三次的列,先从队列二中取,否则从队列一中取。
其取出之后先向上遍历没有放置飞镖的行,在当前列和队首列的同一行放置两个飞镖。放完此时这个点也可以作为que2的一个元素。
非法情况就是用的时候出现了队列为空。
关于贪心的解释:我们可以发现,对于que1和que2,其que2只能用来给弹3次的用(不然就超出了题目每行每列最多两个的限制),而que1既可以给3次的用,又可以给2次的用。所以优先用que2的。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
typedef pair<LL,LL>S;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn];
struct P{
LL x,y;
};
queue<S>que1,que2;
vector<P>ans;
LL row[maxn],col[maxn];
int main(void){
cin.tie(0);std::ios::sync_with_stdio(false);
LL n;cin>>n;
for(LL i=1;i<=n;i++) cin>>a[i];
bool flag=1;
for(LL i=n;i>=1;i--){
if(a[i]==0) continue;
else if(a[i]==1){
que1.push({i,i});
ans.push_back({i,i});
++row[i];++col[i];
}
else if(a[i]==2){
if(que1.empty()){
flag=0;break;
}
S x=que1.front();que1.pop();
ans.push_back({x.first,i});
que2.push({x.first,i});
++row[x.first];++col[i];
}
else if(a[i]==3){
if(!que2.empty()){
S x=que2.front();que2.pop();
LL temp=x.first-1;
while(temp>=1&&row[temp]) temp--;
if(temp<1) break;
ans.push_back({temp,i});
ans.push_back({temp,x.second});
row[temp]+=2;col[i]++;col[x.second]++;
que2.push({temp,i});
}
else if(!que1.empty()){
S x=que1.front();que1.pop();
LL temp=x.first-1;
while(temp>=1&&row[temp]) temp--;
if(temp<=0) break;
ans.push_back({temp,i});
ans.push_back({temp,x.second});
row[temp]+=2;col[i]++;col[x.second]++;
que2.push({temp,i});
}
else{
flag=0;break;
}
}
}
if(flag){
cout<<ans.size()<<"\n";
for(LL i=0;i<ans.size();i++){
cout<<ans[i].x<<" "<<ans[i].y<<"\n";
}
}
else{
cout<<"-1"<<"\n";
}
return 0;
}