Codeforces Global Round 5 C2. Balanced Removals (Harder)(思维)

题目链接
Codeforces Global Round 5 C2. Balanced Removals (Harder)(思维)_第1张图片
Codeforces Global Round 5 C2. Balanced Removals (Harder)(思维)_第2张图片
思路:保证点的顺序时单调的就行,先按xyz排序,然后先删x和y都相同的,再删x相同的,最后删剩下的。

#include 
using namespace std;
const int maxn=5e4+1;
typedef long long ll;
bool vis[maxn];
struct cxk{
	int x,y,z,id;
}s[maxn];
vector<cxk>temp,ans;
bool cmp(const cxk &a,const cxk &b)
{
	if(a.x==b.x) return a.y==b.y?a.z<b.z:a.y<b.y;
	else return a.x<b.x;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%d %d %d",&s[i].x,&s[i].y,&s[i].z),s[i].id=i;
	sort(s+1,s+1+n,cmp);
	for(int i=1;i<n;++i)
	if(s[i].x==s[i+1].x&&s[i].y==s[i+1].y) printf("%d %d\n",s[i].id,s[i+1].id),vis[i+1]=vis[i]=true,i++;
	for(int i=1;i<=n;++i)
	if(!vis[i]) temp.push_back(s[i]);
	memset(vis,false,sizeof(vis));
	for(int i=0;i<temp.size();++i)
	if(temp[i].x==temp[i+1].x) printf("%d %d\n",temp[i].id,temp[i+1].id),vis[i+1]=vis[i]=true,i++;
	for(int i=0;i<temp.size();++i)
	if(!vis[i]) ans.push_back(temp[i]);
	for(int i=0;i<ans.size();++i) printf("%d %d\n",ans[i].id,ans[i+1].id),i++;
 } 

你可能感兴趣的:(思维)