2020杭电多校第七场

hdu-6850 Game(思维题)

#include 
using namespace std;

int _, n;
long long x[2002], y[2002];

struct node {
	int u, v;
	long long w;
}line[4000006];
int vis[2003][2003];
long long mp[2003][2003];

long long cal(int i, int j) {
	return (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
}

bool cmp(node a, node b) {
	return a.w > b.w;
}

void bfs(int pos, long long len) {
	for(int i = 1; i <= n; i++) {
		if(i == pos) continue;
		int fi = min(i, pos);
		int se = max(i, pos);
		if(len > mp[fi][se]) vis[fi][se] = 2;
	}
}


int main() {
	scanf("%d", &_);
	while(_--) {
		memset(vis, 0, sizeof(vis));
		scanf("%d", &n);
		for(int i = 1;i <= n; i++) {
			scanf("%lld %lld", &x[i], &y[i]);
		}
		
		int cnt = 0;
		for(int i = 1; i <= n; i++) {
			for(int j = i+1; j <= n; j++) {
				mp[i][j] = cal(i, j);
				line[++cnt].u = i;
				line[cnt].v = j;
				line[cnt].w = mp[i][j];
			}
		}
		
		sort(line+1, line+1+cnt, cmp);
		
		for(int i = 1;i <= cnt; i++) {
			int u = line[i].u, v = line[i].v;
			if(vis[u][v]) continue;
			vis[u][v] = 1;
			bfs(u, mp[u][v]);
			bfs(v, mp[u][v]);
		}
		
		bool ok = 0;
		for(int i = 2;i <= n; i++) {
			if(vis[1][i] == 1) {
				ok = 1;
				break;
			}
		}
		
		if(ok) puts("YES");
		else puts("NO");
	}
}

hdu-6852 Increasing and Decreasing(构造题)

#include 
using namespace std;

int _, n;
long long a, b;
deque <int> q;
stack <int> sta;

int main() {
	scanf("%d", &_);
	while(_--) {
		scanf("%d %lld %lld", &n, &a, &b);
		if(a+b-1 <= n && n <= a*b) puts("YES");
		else {puts("NO"); continue;}
		
		int sheng = 0, pos = n;
		bool ok = 0;
		while(pos > 0) {
			sheng++;
			for(;pos >= 1; pos--) {
				sta.push(pos);
				if(sta.size()==b || sheng+pos-1==a) break;
			} pos--;
			if(sheng+pos == a) ok = 1;
			
			
			while(!sta.empty()) {
				int t = sta.top(); sta.pop();
				q.push_front(t);
			}
			
			if(ok) {
				for(;pos >= 1; pos--) q.push_front(pos);
			}
		}
		for(int i = 1;i <= n; i++) {
			printf("%d%c", q.front(), i==n?'\n':' ');
			q.pop_front();
		}
	}
}

hdu-6853 Jogging

#include 
using namespace std;

int _;
long long x, y, fenzi, fenmu;
bool ok;

map <pair<long long, long long>, bool> vis;

long long gcd(long long a, long long b) {
	if(b == 0) return a;
	return gcd(b, a%b);
}

int bfs(long long x, long long y) {
	queue <pair<long long, long long> > q;
	q.push(make_pair(x, y));
	vis[make_pair(x, y)] = 1;
	while(!q.empty()) {
		pair <long long, long long> t = q.front(); q.pop();
		for(long long i = -1; i <= 1; i++) {
			for(long long j = -1; j <= 1; j++) {
				long long ti = t.first+i, tj = t.second+j;
				if(gcd(ti, tj) > 1) {
					if(ti == tj) return 0;
					else {
						fenmu++;
						if(!vis[make_pair(ti, tj)]) {
							q.push(make_pair(ti, tj));
							vis[make_pair(ti, tj)] = 1;
						}
						
					}
				}
			}
		}
	}
	return 1;
}

int main() {
	scanf("%d", &_);
	while(_--) {
		ok = 1;
		vis.clear();
		fenzi = 0, fenmu = 0;
		scanf("%lld %lld", &x, &y);
		for(long long i = -1; i <= 1; i++) {
			for(long long j = -1; j <= 1; j++) {
				if(gcd(x+i, y+j) > 1) {
					fenzi++;
				}
			}
		}
		ok = bfs(x, y);
		if(!ok) {puts("0/1"); continue;}
		long long yue = gcd(fenzi, fenmu);
		printf("%lld/%lld\n", fenzi/yue, fenmu/yue);
	}
}

你可能感兴趣的:(BFS,图论,构造)