【NOIP2018 模拟赛day2】棋盘

今天唯一一道会的题,结果超时了。(整个人都炸了)

这道题是直接减的,如果开二维数组会直接爆炸,编译直接报错。

通过我自己在草稿本上推出来的一些东西,打出了50分的代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
const int MAXN=1e6;
int x[MAXN+1];
int y[MAXN+1];
int main() {
	int m,n;
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; i++) {
		x[i]=1;
		y[i]=1;
	}
	while(m--) {
		int a,b;
		cin>>a>>b;
		x[a]=0;
		y[b]=0;
		int totx=0;
		for(int i=1; i<=n; i++)
			if(x[i])
				totx++;
		int toty=0;
		for(int i=1; i<=n; i++)
			if(y[i])
				toty++;
		long long sum=toty*totx;
		printf("%lld\n",sum);
	}
	return 0;
}
然后,在讲题之后的AC代码(其实我想得差不多)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#pragma G++ optimaze (2)
using namespace std;
const int MAXN=1e6;
long long x[MAXN+5];
long long y[MAXN+5];
int main() {
	long long m,n;
	scanf("%lld%lld",&n,&m);

	long long totx=n;
	long long toty=n;
	long long sum=n*n;
	while(m--) {
		int a,b;
		cin>>a>>b;
		if(!x[a]){
			x[a]=1;
			sum-=toty;
			totx--;
		}
		if(!y[b]){
			y[b]=1;
			sum-=totx;
			toty--;
		}
		printf("%lld\n",sum);
	}
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_41734244/article/details/79842353
今日推荐