Educational Codeforces Round 115 (Rated for Div. 2)[A~D]

Posted by mcog_esteban on Mon, 11 Oct 2021 18:52:00 +0200

A

meaning of the title

Give a 2 × n 2 \times n Matrix of 2 x n, starting point ( 1 , 1 ) (1, 1) (1,1) Endpoint ( 2 , n ) (2, n) (2,n), walk to it every time
Adjacent lattices (including diagonals), where 0 represents open space and 1 represents obstacles, ask if you can reach the end

Problem

as long as ( 1 , x ) , ( 2 , x ) (1,x),(2,x) (1,x), (2,x) One can walk at different times

#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
#include<random>
using namespace std;
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#include<ext/rope>
using namespace __gnu_cxx;

#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))

const int mod = 1e9+7;
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;

#define endl '\n'
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		string s1,s2; cin>>s1>>s2;
		int flag=1;
		for(int i=0;i<n;i++)
			if(s1[i]=='1'&&s2[i]=='1')flag=0;
		cout<<(flag?"YES":"NO")<<endl;
	}
}


B

meaning of the title

Yes n n n students ( n n n is even), given for each student 5 5 Five numbers, of which the i i i Number a i a_i ai indicates whether the student can be in the week i i Can students be divided into two groups:

  1. Group 1 students are available on day i I
  2. Group 2 students are available on day j
  3. i   ! = j i\ !=j i !=j

Problem

Considering that there are only five days in total, i , j i,j i, J permutation combination is also 10 cases, so you can enumerate i and j to determine whether it is legal. How can it be legal?

  1. No. i i Number of days 1 C 1 C_1 C1 To ≤ n / 2 \le n/2 Less than n/2, No. j j Number of days 1 C 2 C_2 C2 Also ≤ n / 2 \le n/2 Less than n/2 because it is divided into two groups n / 2 n/2 n/2 must not be in a group
  2. set up i , j i,j i,j The number of days that together are 1 is C 3 C_3 C3, then C 1 + C 2 − C 3 C_1+C_2-C_3 C1 +C2 C3 equals n n n, because it is n n n divided into two groups, then the people in each group together should equal n n n, the sum of the two groups is i , j i,j i,j The sum of the number of people who can go in two days minus the return portion C 3 C_3 C3​

#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
#include<random>
using namespace std;
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#include<ext/rope>
using namespace __gnu_cxx;

#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))

const int mod = 1e9+7;
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;
int a[1004][6];

#define endl '\n'
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=5;j++)
				cin>>a[i][j];
		int flag=0;
		for(int i=1;i<=5;i++){
			for(int j=1;j<=5;j++){
				if(i==j)continue;
				int c1=0,c2=0,c3=0;
		//c1 is the number of days 1, c2 is the number of days 1, and c3 is the number of both i and j being 1
				for(int k=1;k<=n;k++){
					if(a[k][i])c1++;
					if(a[k][j])c2++;
					if(a[k][i]&&a[k][j])c3++;
				}
				if(c1>=n/2&&c2>=n/2&&c1+c2-c3>=n)flag=1;
			}
		}
		cout<<(flag?"YES":"NO")<<endl;
	}
}


C

meaning of the title

Give a sequence a [ 1... n ] a[1...n] a[1...n], set the sequence average to be k k k, find how many pairs there are ( i , j ) (i, j) (i,j) delete the sequence a [ i ] , a [ j ] a[i],a[j] The mean after a[i], a[j] does not change

Problem

map records the number of occurrences of each number, then enumerates a [ i ] a[i] a[i], to k k k unchanged then a [ j ] = 2 × k − a [ i ] a[j]=2\times k-a[i] a[j]=2 x k_a[i], then a n s + = m a p [ a [ j ] ] ans+=map[ a[j] ] ans+=map[a[j]]

#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
#include<random>
using namespace std;
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#include<ext/rope>
using namespace __gnu_cxx;

#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))

const int mod = 1e9+7;
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;
int a[N];
map<int,int>mp;

#define endl '\n'
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int t; cin>>t;
	while(t--){
		mp.clear();
		int n; cin>>n;
		double k=0;
		for(int i=1;i<=n;i++)cin>>a[i],k+=a[i],mp[a[i]]++;
		k=1.0*k/n;
		int ans=0;
		for(int i=1;i<=n;i++){
			mp[a[i]]--; 
			//Because the enumeration is a[i], in order to prevent aggravation, first reduce the number of a[i] by 1
			double g=k*2-a[i];//Find a[j]
			if(g!=(int)g)continue;//a[j] is not an integer, not legal
			ans+=mp[(int)g];//Remember to override, a[j] is a double type
		}
		cout<<ans<<endl;
	}
}


D

meaning of the title

Yes N N N projects, each i i i There are two properties: project theme a i a_i ai and project difficulty b i b_i bi​
Find how many pairs ( i , j , k ) (i, j, k) (i,j,k), at least one of the following (all satisfied)

  1. a i   ! = a j   ! = a k a_i\ !=a_j\ !=a_k ai​ !=aj​ !=ak​
  2. b i   ! = b j   ! = b k b_i\ !=b_j\ !=b_k bi​ !=bj​ !=bk​

Problem

It is cumbersome to think positively about how many legitimate situations there are: to calculate what satisfies the first, what satisfies the second, and to subtract the coincident part - and what satisfies the second. To think negatively, subtract the illegal part from the total a a a has the same, and b b b The same is true.

So we can think about it this way, enumerate two a a a Equal i , j i,j i,j (note that not three, three words) b b b It's all different), and then see how many b k = = b i b_k==b_i bk ==bi or how many b k = = b j b_k==b_j bk ==bj, but enumerated i , j i,j i,j must have timed out, but we noticed b i b_i bi and b j b_j The count of bj is independent if enumerated a a a Common C a C_a Ca, then for i i i, there will be C a − 1 C_a-1 Ca 1 Calculation b k = = b i b_k==b_i bk ==bi, for example, if C a = 4 C_a=4 Ca = 4, that enumeration i , j i,j i,j:

  1. i = 1 , j = 2 i=1,j=2 i=1,j=2
  2. i = 1 , j = 3 i=1,j=3 i=1,j=3
  3. i = 1 , j = 4 i=1,j=4 i=1,j=4
  4. i = 2 , j = 3 i=2,j=3 i=2,j=3
  5. i = 2 , j = 4 i=2,j=4 i=2,j=4
  6. i = 3 , j = 4 i=3,j=4 i=3,j=4

It is not difficult to find out that calculation is needed in times 1, 2 and 3. b k = = b i 1 b_k==b_{i_1} bk ==bi1, need to be calculated in order of (4) b k = = b i 2 b_k==b_{i_2} bk ==bi2, need to be calculated in order of (4) b k = = b i 3 b_k==b_{i_3} bk ==bi3, need to be calculated in the order of 3_ b k = = b i 4 b_k==b_{i_4} bk​==bi4​​

#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
#include<random>
using namespace std;
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#include<ext/rope>
using namespace __gnu_cxx;

#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))

const int mod = 1e9+7;
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;
int ca[N],cb[N];
vector<int>g[N]; 
#define endl '\n'
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		for(int i=1;i<=n;i++)g[i].clear();
		for(int i=1;i<=n;i++)ca[i]=cb[i]=0;
		for(int i=1;i<=n;i++){
			int a,b; cin>>a>>b;
			ca[a]++,cb[b]++;//ca is the number of occurrences of a and cb is the number of occurrences of b
			g[a].push_back(b);//Record how many b each a has
		}
		int tot=n*(n-1)*(n-2)/6;//Total
		for(int i=1;i<=n;i++){
			if(ca[i]<2) continue;
			//If the number of times i is less than 2, then a is not in an illegal state
			for(int j=0;j<ca[i];j++)
				tot-=(ca[i]-1)*(cb[g[i][j]]-1);
		}
		cout<<tot<<endl;
	}
}

Topics: Algorithm