CF#744 Div.3 & 2020 ICPC Shenyang Station

Keywords: C++ Algorithm ICPC

Pro

Codeforces Round #744 (Div. 3)

The 2020 ICPC Asia Shenyang Regional Programming Contest

Supplementary questions, not all questions, please see problem name

Sol

C. Ticks

Forgot to look at this question during the match and enumerate each one ∗ * , see this ∗ * _Whether it is possible to form two edges with an Edge Length greater than or equal to k.

Give to all who pass by if you can ∗ * Mark and end up with all ∗ * Marked to see if it's OK.

D. Productive Meeting

At first it came to mind that the first and second largest together are greedy best, but only compare the first and second largest.

That is, the correct greedy strategy for this topic is: the largest and second largest conversations are the best each time. The greedy strategy does not want to be complete.

E2. Array Optimization by Deque

This topic only has more inversion pairs than E1. Here I write an inversion pair for a tree array.

So each time you add 1 directly to the position of the value in the tree array, you can decide whether it should be placed before or after.

If put in front, the corresponding is demand 1 ∼ a [ i ] − 1 1\sim a[i]-1 Sum of 1_a[i]1;

If put behind, the corresponding is demand 1 ∼ m 1\sim m Sum and of 1_m (m is the maximum value of input data) 1 ∼ a [ i ] 1\sim a[i] Sum of 1_a[i], the difference between the two.

Next, find a min ute for each of the above, and add up to the answer.

F. Array Stabilization (AND version)

Because Yes, 0 participates in and operates to make the answer zero, so save the position of 0.

The process of moving is then simulated and repeated if the new result is still 0.

Finally, determine if the array is all zero.

D. Journey to Un'Goro

Search plus pruning, but still thinking.

Looking at the first question first, when all are r, the result must be the largest, just ask for the answer when all are R.

Consider b as 0 and r as 1. This is the case when r is odd, that is, the interval XOR and NON are 0.

set up a i a_i ai means the length is i i I, the number of r, then the number of R in the interval (i,j) a j − a i − 1 a_j-a_{i-1} aj​−ai−1​

So when a j a_j aj and a i − 1 a_{i-1} When the parity of ai_1 is different, the number of r is odd.

Notice that the a 0 a_0 a0 is accessible because if you don't take the first position, you won't go in.

Here a 0 a_0 The meaning of a0 is that the prefix is empty and the number of r is 0.

It's easy to think that when all are r, the result must be the largest, so consider the answer when all are R.

There are n+1 alternatives (because there are a 0 a_0 a0), so according to a + b = = m , a × b ≤ ⌊ m 2 ⌋ ⌈ m 2 ⌉ a+b==m,a\times b\le \lfloor \frac{m}{2}\rfloor \lceil \frac{m}{2} \rceil a+b==m,a*b < 2m 2m, so the first answer is: ⌊ n + 1 2 ⌋ ⌈ n + 1 2 ⌉ \lfloor \frac{n+1}{2}\rfloor \lceil \frac{n+1}{2} \rceil ⌊2n+1​⌋⌈2n+1​⌉

Watch out for long long!!!

Second, since only 100 are output, the complexity of the search is acceptable.

I don't use XOR here because I feel that direct judgment is more intuitive.

Upload/cy Later

Code

C. Ticks

//By cls1277
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<set>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define lowbit(_) _&(-_)
#define ls x<<1
#define rs x<<1|1
#define endl '\n'
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

const LL maxn = 25;
int T , n , m , k;
char mp[maxn][maxn]; 
vector<pair<int,int>>a;
bool b[maxn][maxn];

int main() {
	ios::sync_with_stdio(false);
	#ifdef DEBUG
	freopen("data.txt","r",stdin);
	#endif
	cin>>T;
	while(T--) {
		cin>>n>>m>>k;
		Ms(b,0); a.clear();
		Fo(i,1,n)
			Fo(j,1,m) {
				cin>>mp[i][j];
				if(mp[i][j]=='*') a.push_back(make_pair(i,j));
			}
		for(int i=0; i<a.size(); i++) {
			int ux=a[i].first , uy=a[i].second;
			if(ux-k<=0||uy-k<=0||uy+k>m) continue;
			int cnt=1;
			while(mp[ux-cnt][uy-cnt]=='*'&&mp[ux-cnt][uy+cnt]=='*')
				cnt++;
			cnt--;
			if(cnt>=k) {
				b[ux][uy] = 1;
				while(cnt) b[ux-cnt][uy-cnt]=b[ux-cnt][uy+cnt]=1,cnt--; 
			}
		}
		int flag = 0;
		for(int i=0; i<a.size(); i++)
			if(b[a[i].first][a[i].second]==0) {
				flag = 1;
				break;
			}
		if(flag) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
	return 0;
}

D. Productive Meeting

//By cls1277
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<set>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define lowbit(_) _&(-_)
#define ls x<<1
#define rs x<<1|1
#define endl '\n'
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

const LL maxn = 1e5+5;
int T , n , tot;
pair<int,int>ans[maxn];
priority_queue<pair<int,int>>q;

int main() {
	ios::sync_with_stdio(false);
	#ifdef DEBUG
	freopen("data.txt","r",stdin);
	#endif
	cin>>T;
	while(T--) {
		cin>>n; tot=0;
		while(!q.empty()) q.pop();
		Fo(i,1,n) {
			int x; cin>>x;
			if(x) q.push(make_pair(x,i));
		}
		while(q.size()>1) {
			pair<int,int> u=q.top(); q.pop();
			pair<int,int> v=q.top(); q.pop();
			ans[++tot] = make_pair(u.second,v.second);
			v.first--; u.first--;
			if(v.first) q.push(v);
			if(u.first) q.push(u); 
		}
		cout<<tot<<endl;
		Fo(i,1,tot) cout<<ans[i].first<<" "<<ans[i].second<<endl;
	}
	return 0;
}

E2. Array Optimization by Deque

//By cls1277
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<set>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define lowbit(_) _&(-_)
#define ls x<<1
#define rs x<<1|1
#define endl '\n'
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

const LL maxn = 2e5+5;
LL T , n , m , a[maxn] , b[maxn] , ans; 

struct BIT {
	LL tree[maxn];
	void add(LL x , LL y) {
		while(x<=n) {
			tree[x]+=y;
			x+=lowbit(x);
		}
		return ;
	}
	LL sum(LL x) {
		LL res=0;
		while(x) {
			res+=tree[x];
			x-=lowbit(x);
		}
		return res;
	}
}cls; 

int main() {
	ios::sync_with_stdio(false);
	#ifdef DEBUG
	freopen("data.txt","r",stdin);
	#endif
	cin>>T;
	while(T--) {
		cin>>n;
		Ms(cls.tree,0); ans=0;
		Fo(i,1,n) {
			cin>>a[i];
			b[i] = a[i];
		}
		sort(b+1,b+n+1);
		m=unique(b+1,b+n+1)-b-1;
		Fo(i,1,n)
			a[i]=lower_bound(b+1,b+m+1,a[i])-b;
		Fo(i,1,n) {
			cls.add(a[i],1);
			ans+=min(cls.sum(a[i]-1),cls.sum(m)-cls.sum(a[i]));
		}
		cout<<ans<<endl;
	}
	return 0;
}

F. Array Stabilization (AND version)

//By cls1277
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<set>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define lowbit(_) _&(-_)
#define ls x<<1
#define rs x<<1|1
#define endl '\n'
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

const LL maxn = 1e6+5;
int T , n , a[maxn] , d , ans;
queue<pair<int,int>>q;

int main() {
	ios::sync_with_stdio(false);
	#ifdef DEBUG
	freopen("data.txt","r",stdin);
	#endif
	cin>>T;
	while(T--) {
		ans=0;
		cin>>n>>d; int flag=0;
		Fo(i,0,n-1) cin>>a[i];
		Fo(i,0,n-1) if(!a[i]) q.push(make_pair(0,i));
		while(!q.empty()) {
			pair<int,int>u=q.front(); q.pop();
			int ops = (u.second+d)%n;
			if(a[ops]) {
				a[ops]=0;
				q.push(make_pair(u.first+1,ops));
				ans = max(ans,u.first+1);
			}
		}
		Fo(i,0,n-1) if(a[i]) {
			flag = 1;
			break;
		}
		if(flag) cout<<"-1"<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}

D. Journey to Un'Goro

//By cls1277
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<set>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define lowbit(_) _&(-_)
#define mk(_,__) make_pair(_,__)
#define pii pair<int,int>
#define ls x<<1
#define rs x<<1|1
#define endl '\n'
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

const LL maxn = 1e5+5;
int n , cnt;
char s[maxn];

void dfs(int x , int cnt0, int cnt1 , int z) {
	if(cnt0>(n+2)/2||cnt1>(n+2)/2) return ;
	if(x==n) {
		cout<<s<<endl;
		if(++cnt>=100)
			exit(0);	
		return ;
	}
	s[x]='b';
	if(!z) dfs(x+1,cnt0+1,cnt1,0);
	else dfs(x+1,cnt0,cnt1+1,1);
	s[x]='r';
	if(!z) dfs(x+1,cnt0,cnt1+1,1);
	else dfs(x+1,cnt0+1,cnt1,0);
	return ;
}

int main() {
	ios::sync_with_stdio(false);
	#ifdef DEBUG
	freopen("data.txt","r",stdin);
	#endif
	cin>>n;
	cout<<1LL*((n+1)/2)*((n+2)/2)<<endl;
	dfs(0,1,0,0);
	return 0;
}

F. Kobolds and Catacombs

//By cls1277
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<set>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define lowbit(_) _&(-_)
#define mk(_,__) make_pair(_,__)
#define pii pair<int,int>
#define ls x<<1
#define rs x<<1|1
#define endl '\n'
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

const LL maxn = 1e6+5;
int n , a[maxn] , c[maxn] , ans;
pii b[maxn];

int main() {
	ios::sync_with_stdio(false);
	#ifdef DEBUG
	freopen("data.txt","r",stdin);
	#endif
	cin>>n;
	Fo(i,1,n) {
		cin>>a[i];
		b[i] = mk(a[i],i);
	}
	sort(b+1,b+n+1);
	Fo(i,1,n) c[b[i].second]=i;
	//Fo(i,1,n) cout<<c[i]<<" ";
	Fo(i,1,n) {
		int r=c[i] , y;
		do {
			y=r;
			Fo(j,i,y) r=max(r,c[j]);			
		}while(r!=y);
		i=r;
		ans++;
	}
	cout<<ans;
	return 0;
}

G. The Witchwood

//By cls1277
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<set>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define lowbit(_) _&(-_)
#define ls x<<1
#define rs x<<1|1
#define endl '\n'
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

const LL maxn = 1005;
LL n , k , ans , a[maxn];

int main() {
	ios::sync_with_stdio(false);
	#ifdef DEBUG
	freopen("data.txt","r",stdin);
	#endif
	cin>>n>>k;
	Fo(i,1,n) cin>>a[i];
	sort(a+1,a+n+1);
	Ro(i,n,n-k+1) ans+=a[i];
	cout<<ans;
	return 0;
}

K. Scholomance Academy

//By cls1277
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<sstream>
#include<set>
#include<cassert>
#include<bitset>
using namespace std;
typedef long long LL;
#define PI acos(-1)
#define INF 2147483647
#define eps 1e-7
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define lowbit(_) _&(-_)
#define mk(_,__) make_pair(_,__)
#define pii pair<int,int>
#define ls x<<1
#define rs x<<1|1
#define endl '\n'
inline LL read() {
	LL x = 0, f = 1;char c = getchar();
	while (!isdigit(c)) { if (c == '-')f = -f;c = getchar(); }
	while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48ll), c = getchar();
	return x * f;
}

//const LL maxn = ;
int n,tp,fn,fp,tn;
double pre , ans;
struct Node {
	char c;
	int s;
	Node(){};
	Node(char cc , int ss) {
		c=cc , s=ss;
	}
	bool operator < (const Node &a) {
		if(s==a.s) return c>a.c;
		return s>a.s;
	}
};
vector<Node>a;

double tpr() {
	return tp*1.0/(tp+fn);
}

double fpr() {
	return fp*1.0/(tn+fp);
}

int main() {
	//ios::sync_with_stdio(false);
	#ifdef DEBUG
	freopen("data.txt","r",stdin);
	#endif
	scanf("%d\n",&n);
	Fo(i,1,n) {
		char ch; int sc;
		scanf("%c%d\n",&ch,&sc);
		a.push_back(Node(ch,sc));
		if(ch=='+') fn++;
		else tn++; 
	}
	sort(a.begin(),a.end());
	for(int i=0; i<a.size(); i++) {
		if(a[i].c=='+') fn--,tp++;
		else tn--,fp++;
		ans+=tpr()*(fpr()-pre);
		pre=fpr();
	}
	printf("%.10lf",ans);
	return 0;
}

Posted by bobleny on Thu, 14 Oct 2021 10:58:10 -0700