Codeforces Round #599 (Div. 2) 题解

题目链接:https://codeforces.com/contest/1243

 

A - Maximum Square

水题不说了

#include 
using namespace std;
const int mx = 1e5 + 50;
typedef long long ll;
int n; 
int a[mx];
int main() {
	int t;
	scanf("%d",&t);
	while (t--) {
		scanf("%d",&n);
		for (int i=1;i<=n;i++) {
			scanf("%d",a+i);
		}
		sort(a+1,a+1+n);
		int mi = 1e7;
		int ans = -1;
		for (int i=n;i>=1;i--) {
			mi = min(mi,a[i]);
			ans = max(ans,min(mi,n-i+1));
		}
		printf("%d\n",ans);
	} 
	return 0;
}

B2 - Character Swap (Hard Version)

固定第一个字符串,然后第二个字符串的当前位置要和第一个串一样,至多用后面的字符换2次就好了,所以是2*n次

#include 
#define fi first
#define se second
using namespace std;
const int mx = 100 + 50;
typedef long long ll;
typedef pair pa;
int n; 
char s1[mx],s2[mx];
int vis[mx];
pa ans[mx];
int siz;
pa find_pos(int x) {
	for (int i=x+1;i<=n;i++) 
		if (s1[i]==s1[x])
			return pa(0,i);
		else if (s2[i]==s1[x])
			return pa(1,i);
}
bool solve() {
	for (int i=0;i<26;i++) if(vis[i]%2!=0)
		return 0;
	for (int i=1;i<=n;i++) {
		if (s1[i]==s2[i]) continue;
		pa now = find_pos(i);
		if (now.fi == 0) {
			swap(s2[i],s1[now.se]);
			ans[siz++] = pa(now.se,i);
		} else {
			swap(s1[n],s2[now.se]);
			swap(s1[n],s2[i]);
			ans[siz++] = pa(n,now.se);
			ans[siz++] = pa(n,i);
		}
	}
	return 1;
}
int main() {
	int t;
	scanf("%d",&t);
	while (t--) {
		scanf("%d",&n);
		scanf("%s%s",s1+1,s2+1);
		siz = 0;
		memset(vis,0,sizeof(vis));
		for (int i=1;s1[i];i++)
			vis[s1[i]-'a']++;
		for (int i=1;s2[i];i++)
			vis[s2[i]-'a']++;
		if(!solve()) puts("No");
		else {
			printf("Yes\n%d\n",siz);
			for (int i=0;i

C - Tile Painting

超过一个质数肯定都一样,因为肯定有x使得a*p1 - b*p2 == 1,所以i和i-1颜色一样。

然后一个质数答案就是那个质数了

#include 
#define fi first
#define se second
using namespace std;
const int mx = 1e6 + 10;
typedef long long ll;
typedef pair pa;
ll n; 
vector  pri;
bool vis[mx];
void init() {
	for (int i=2;i= mx) break;
			vis[i*j] = 1;
			if (i%j==0) break;
		}
	}
}
int main() {
	init();
	ll n;
	int cnt = 0;
	scanf("%lld",&n);
	ll ans = n;
	for (int i:pri) {
		if (i >= n) break;
		if (n % i == 0) {
			if (cnt) return 0*puts("1");
			cnt = i;
			ans = cnt;
		}
		while (n % i == 0) n /= i;
	}	
	if (cnt && n != 1) return 0*puts("1");
	printf("%lld\n",ans);
	return 0;
}

D - 0-1 MST

暴力

#include 
#define fi first
#define se second
using namespace std;
const int mx = 1e5 + 10;
typedef long long ll;
typedef pair pa;
int n,m;
map  mp[mx];
set  st,nt;
void dfs(int u) {
	nt.clear();
	vector  g;
	for (int v:st) {
		if (!mp[u][v]) 
			g.push_back(v);
		else
			nt.insert(v);
	}
	st = nt;
	for (int v:g)
		dfs(v);
}
int main() {
	scanf("%d%d",&n,&m);
	int u,v;
	for (int i=0;i

E - Sum Balance

连下边,然后跑一跑环就ok了,注意是简单环

#include
#define fi first
#define se second
using namespace std;
typedef pair pa;
typedef long long ll;
const int mx = 100 + 10;
int m;
ll num[mx];
vector  g[mx],r[1<<18];
map  mp;
map  mvis;
stack  sta;
pa s[mx*mx];
int vis[1<<18],siz;
bool vis2[1<<18];
pa ans[mx];
ll reg;
int get_ed(int x) {
	ll y = reg - num[mp[x]] + x;
	return mp.count(y)? y : -1; 
}
void full (int u) {
	int y = 0;
	bool mark[20];
	memset(mark,0,sizeof(mark));
	while (sta.top() != u){
		int v = sta.top();
		sta.pop();
		if (mark[mp[v]]) return ;
		mark[mp[v]] = 1;
		y |= 1<<(mp[v]-1);
	}
	if (mark[mp[u]]) return ;
	y |= 1<<(mp[u]-1);
	if (vis2[y]) return ;
	vis2[y] = 1;
	s[siz++] = pa(y,u);
}
void dfs(int u) {
	if (mvis[u] == 2) return ;
	if (mvis[u] == 1) {
		full(u);
		return ;
	}
	mvis[u] = 1;
	sta.push(u);
	int v = get_ed(u);
	if (v != -1) dfs(v);
	mvis[u] = 2;
}
int main() {
	ll sum = 0;
	int cnt = 0;
	scanf("%d",&m);
	for (int i=1;i<=m;i++) {
		int c,u;
		scanf("%d",&c);
		for (int j=0;j

 

你可能感兴趣的:(图论,优雅暴力,建设性)