Codeforces Round #690 (Div. 3)

Nikhil

2020/12/17

It was the first round in which I managed to solve 6 problems out of 7, submitting the last one in last 3 seconds. Following are some thoughts I had while solving those problems and some mistakes I made. I make it a point to refer the notebook I use for writing observations and calculations during contest for writing these blogs. So that even if I delay the post by a day or two I don’t miss much on the content. So let’s start with the first problem.

Solved during contest

A. Favorite sequence

deque<int> q;
for(int i=0;i<n;i++){
	cin>>x;
	q.push_back(x);
}
for(int i=1;i<=n;i++){
	if(i&1){cout<<q.front()<<" ";q.pop_front();}
	else{cout<<q.back()<<" ";q.pop_back();}
}

B. Last Year’s Substring

for(len=0;len<=4;len++){
	string prefix=s.substr(0,len);
	string suffix=s.substr(n-len);

	if((prefix+suffix)=="2020"){puts("YES");return;}
}
puts("NO");

C. Unique Number

void dfs(string s){
	int sum=0;
	for(auto i:s)sum+=(i-'0');
	if(sum>50)return;
	if(dp[sum]!=-1)dp[sum]=min(dp[sum],stoll(s));
	else{
		if(!s.empty())dp[sum]=stoll(s);
	}
	
	int mx='0';if(!s.empty())mx=s.back();
	for(char i=mx+1;i<='9';i++){
		dfs(s+i);
	}
}

D. Add to Neighbour and Remove

int ans=n-1;
set<int> s;
vector<int> p(n+1);
for(int i=1;i<=n;i++){
	p[i]=p[i-1]+a[i-1];
	s.insert(p[i]);
}
for(int i=1;i<=n;i++){
	int x=p[i];
	if(p[n]%x!=0)continue; //first observation
	bool ok=1;
	int parts=0;
	for(int cur=x;cur<=p[n];cur+=x){
		parts++;
		ok&=(s.find(cur)!=s.end());
	}
	int moves=n-parts;
	if(ok)ans=min(ans,moves);
}

E1. Close Tuples (easy version)

reverse(all(a));
for(int i=0;i<n;i++){
	ans+=f[a[i]+1]*f[a[i]];
	ans+=f[a[i]-1]*f[a[i]];
	ans+=f[a[i]+2]*f[a[i]];
	ans+=f[a[i]+1]*f[a[i]+2];
	ans+=f[a[i]+1]*f[a[i]-1];
	if(a[i]>=2)ans+=f[a[i]-2]*(f[a[i]]+f[a[i]-1]);
	if(a[i]>=2)ans+=(f[a[i]-2]*(f[a[i]-2]-1))/2;

	for(int k=-1;k<=2;k++)ans+=(f[a[i]+k]*(f[a[i]+k]-1))/2;
	f[a[i]]++;
}

F. The Treasure of The Segments

Solved after contest

E2. Close Tuples (hard version)