Contest: Link
Contestants with higher scores will be classified first (we need to sort contestants by their scores). A tie occurs when contestants have the same score. We want to avoid resorting to penalty time, which happens when there is a tie between the last classified contestant and the first non-classified contestant. Therefore, we only need to check if the $$$m^{th}$$$ contestant and the $$$(m+1)^{th}$$$ contestant have the same score.
void solve(){
read(n,m);
vi v(n);read(v);
sort(rall(v));
if(v[m]==v[m-1])ps("EMPATE");
else ps("BIEN");
}
Suppose $$$k$$$ is a number that divides all $$$a_i$$$ integers. If any $$$a_i = k$$$, that's our answer. Notice that $$$k$$$ can't be less than min $$$a_i$$$. Actually, $$$k$$$ must be equal to min $$$a_i$$$ and also must divide all the elements in the array in order to solution to exist. It is not necessary to calculate the gcd of all numbers here.
void solve(){
read(n);
vi v(n); read(v);
int vl = *min_element(all(v));
fori(i,n){if(v[i]%vl!=0){ps("NO");return;}}
ps("SI");
}
Probably C > D. So we have these scary things running on the line, facing one of the two marks. If we iterate from the leftmost quetas to the rightmost quetas, we can keep track of how many quetas are running to the right. If the current queta is running to the left and there are no quetas running to the right, then ans++, otherwise, decrease the count of quetas running to the right. Finally, add to your answer the remaining number of quetas running to the right.
void solve(){
read(n,a,b);
vii v(n);
fori(i,n)read(v[i].ff);
fori(i,n)read(v[i].ss);
sort(all(v));
int cnt = 0,ans=0;
fori(i,n){
if(v[i].ss==1){ //left
if(cnt==0)ans++;
else cnt--;
}
else{ //right
cnt++;
}
}
ps(ans+cnt);
}
If we priorize the more valued coins, the rest of the price will be the answer.
Proof of correctness
Proved by accepted:
void solve(){
read(n,m);
vi v(n); read(v);
sort(rall(v));
fori(i,n) if(v[i]<=m)m-=v[i];
ps(m);
}
How can we express the number of 3 cells with such restriction in other way? We can express it by complement: number of ways to select 3 cells with such restrictions is the total number of ways to select 3 cells minus the number of ways to select 3 cells that don't follow the restriction.
The total number of ways to select 3 cells can be calculated using combinatory $$$\binom{N}{3} = \frac{n \cdot (n - 1) \cdot (n - 2)}{6}$$$
About the number of ways to select 3 cells that don't follow the restriction, note that for every edge, you have two connected cells and n-2 options to choose the third node. But we have repetions here so for every edge you need substract the current number of connected cells to the current cell.
void solve(){
read(n);
vvi adj(n);
vb vis(n);
int ans = n*(n-1)*(n-2)/6;
fori(i,n-1){
read(u,v);
if(vis[u]) ans -= n-2 - adj[u].size();
else if(vis[v]) ans -= n-2 - adj[v].size();
else ans -= n-2;
vis[u] = vis[v] = true;
adj[u].pb(v);
adj[v].pb(u);
}
ps(ans);
}
In this problem my initial approach was something like $$$nm - (an + bm - ab) = k$$$, where $$$a$$$ is the number of painted rows and $$$b$$$ the number of painted columns. After ordering like $$$b = (n*m-n*a-k)/(m-a)$$$, probably many people (like me) checked if $$$b$$$ follows $$$0\le b \le m$$$. Because of the constraints, this O(N) approach is not enough fast. Notice that $$$k = a1*a2*a3*a4$$$ and $$$2 \le a_i \le 2^{15}$$$, so $$$k$$$ doesn't contain prime factors greater than $$$2^15 \sim 3300$$$, soooo we can build a sieve, then factorize $$$k$$$ and finally get all divisors of $$$k$$$ by its fatorization. I dunno if the last step has an specific name, but u can checked below as the ~ function. As the constraints are still too high on the number of test cases, you need to store the divisors for any given $$$k$$$.
int maxn = 33000;
vi primes;
vb isprime(maxn,true);
void sieve(){
isprime[0]=isprime[1]=false;
for(int i=2;i<maxn;i++){
if(isprime[i]){
primes.pb(i);
for(int j=i*i;j<maxn;j+=i)isprime[j]=false;
}
}
}
vii fact(int num){
vii vc;
for(int p: primes){
if(num==1)break;
int cnt = 0;
while(num%p==0){cnt++;num/=p;}
if(cnt>0)vc.pb({p,cnt});
}
if(num>1)vc.pb({num,1});
return vc;
}
vi get_div(int num){
vii fp = fact(num);
vi ans(1,1);
fori(i,fp.size()){
int sz = ans.size();
int vl = 1;
fori(j,fp[i].ss){
vl *= fp[i].ff;
fori(k,sz)ans.pb(ans[k]*vl);
}
}
return ans;
}
void solve(){
read(n,m,k);
vi vc = get_div(k);
sort(all(vc));
int id = upper_bound(all(vc),n)-vc.begin()-1;
int a = vc[id];
int b = k/a;
if(b<=m){yesi;return;}
else nosi;
}
Extra proof that you need to store vector of divisors for the same $$$k$$$
Proved by TLE (>_<;)
I hope you found it useful ^-^