A - Circle of Students

/* Algorithm: Author: anthony1314 Creat Time: Time Complexity: */

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cstring>
#include<cstdio>
#include<bits/stdc++.h>
#define ll long long
#define maxn 1005
#define mod 1e9 + 7
#define line printf("--------------");
using namespace std;
int a[2005] ;
int main() {
	int t;
	cin>>t;
	while(t--) {
		int n;
		cin>>n;
		int minn = 0;
		for(int i = 0; i < n; i++) {
			cin>>a[i];
			a[i+n] = a[i];
		}
		int k = 0;
		for(int i = 0; i < n; i++) {
			int f = 0;
			for(int j = i; j < i+n-1; j++) {
				if(a[j] - a[j+1] != 1) {
					f = 1;
					break;
				}
			}
			if(f == 0) {
				k = 1;
				cout<<"YES"<<endl;
				break;
			}
			f = 0;
			for(int j = i; j < i+n-1; j++) {
				if(a[j] - a[j+1] != -1) {
					f = 1;
					break;
				}
			}
			if(f == 0) {
				k = 1;
				cout<<"YES"<<endl;
				break;
			}
		}
		if(k == 0) {
			cout<<"NO"<<endl;
		}
	}
	return 0;
}

B - Equal Rectangles

/* Algorithm: Author: anthony1314 Creat Time: Time Complexity: */
 
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cstring>
#include<cstdio>
//#include<bits/stdc++.h>
#define ll long long
#define maxn 1005
#define mod 1e9 + 7
#define line printf("--------------");
using namespace std;
vector<int> q;
int main() {
	int t, n;
	int ss[10005];
	cin>>t;
	while(t--) {
		q.clear();
		cin>>n;
		memset(ss, 0, sizeof(ss));
		int x;
		for(int i = 0; i < 4*n; i++) {
			scanf("%d", &x);
			ss[x]++;
		}
		for(int i = 1; i <= 10000; i++) {
			ss[i] /= 2;
			while(ss[i]--) {
				q.push_back(i);
			}
		}
		sort(q.begin(), q.end());
		int len = q.size();
		int ans = q[0] * q[len-1];
		for(int i = 1; i < len/2; i++) {
			if(q[i] * q[len-i-1] != ans) {
				ans = -1;
				break;
			}
		}
		if(ans == -1)
		{
			cout<<"NO"<<endl;
		} else {
			cout<<"YES"<<endl;
		}
	}
 
 
	return 0;
}

C - Common Divisors

/* Algorithm: Author: anthony1314 Creat Time: Time Complexity: */
 
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cstring>
#include<cstdio>
#include<bits/stdc++.h>
#define ll long long
#define maxn 1005
#define mod 1e9 + 7
#define line printf("--------------");
using namespace std;
ll s[400000];
int main() {
	ll ans;
	int n;
	cin>>n;
	ll x, y;
	scanf("%lld", &x);
	for(int i = 0; i < n-1; i++) {
		scanf("%lld", &y);
		x = __gcd(x, y);
	}
	ans = 0;
	for(ll i = 1; i <= sqrt(x); i++) {
		if(x % i == 0 && i*i != x) {
			ans += 2;
		}else if(i*i == x){
			ans++;
		} 
	}
	cout<<ans<<endl;
	return 0;
}

D - Remove the Substring

/* Algorithm: Author: anthony1314 Creat Time: Time Complexity: */
 
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cstring>
#include<cstdio>
//#include<bits/stdc++.h>
#define ll long long
#define maxn 200005
#define mod 1e9 + 7
#define line printf("--------------");
using namespace std;
char s[maxn], t[maxn];
int pre[maxn], suf[maxn];
int main() {
	cin>>s>>t;
	int lens = strlen(s);
	int lent = strlen(t);
	int cur = 0;
	for(int i = 0; cur < lent; i++) {
		if(s[i] == t[cur]) {
			pre[cur++] = i;
		}
	}
	int index = cur-1;
	cur = lent;
	for(int i = lens; i >= 0; i--) {
		if(s[i] == t[cur-1]) {
			suf[--cur] = i;
		}
	}
	int ans = max(lens-1-pre[index], suf[0]);
	for(int i = 0; i+1 < lent; i++) {
		ans = max(ans, suf[i+1]-pre[i]-1);
	}
	cout<<ans<<endl;
	return 0;
}

E - Boxers

/* Algorithm: Author: anthony1314 Creat Time: Time Complexity: */
 
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cstring>
#include<cstdio>
#include<bits/stdc++.h>
#define ll long long
#define maxn 1005
#define mod 1e9 + 7
#define line printf("--------------");
using namespace std;
ll a[150005];int num[150000+6] = {0};
int main() {
	ll ans = 0;
	int n;
	cin>>n;
	for(int i = 0; i < n; i++) {
		cin>>a[i];
	}
	sort(a, a+n);
	for(int i = 0; i < n; i++) {
		if(a[i] > 1) {
			if(num[a[i]-1] == 0) {
				num[a[i]-1] = 1;
				ans++;
			} else if(num[a[i]] == 0) {
				num[a[i]] = 1;
				ans++;
			} else if(num[a[i]+1] == 0) {
				num[a[i]+1] = 1;
				ans++;
			}
		} else {
			if(num[a[i]] == 0) {
				num[a[i]] = 1;
				ans++;
			} else if(num[a[i]+1] == 0) {
				num[a[i]+1] = 1;
				ans++;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

F - Complete the Projects

/* Algorithm: Author: anthony1314 Creat Time: Time Complexity: */
 
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cstring>
#include<cstdio>
#include<bits/stdc++.h>
#define ll long long
#define maxn 200005
#define mod 1e9 + 7
#define line printf("--------------\n");
using namespace std;
typedef pair<int,int> pii;
vector<pii> pos, neg;
int dp[maxn];
int main() {
	int n, r, ans = 0, a, b;
	cin>>n>>r;
	for(int i = 0; i < n; i++) {
		cin>>a>>b;
		if(b >= 0) {
			pos.push_back(make_pair(a, b));
		} else {
			neg.push_back(make_pair(a+b, a));
		}
	}
	sort(pos.begin(), pos.end());
	sort(neg.begin(), neg.end());
	reverse(neg.begin(), neg.end());
	for(int i = 0; i < pos.size(); i++) {
		a = pos[i].first;
		b = pos[i].second;
		if(a > r) {
			break;
		}
		ans++;
		r += b;
	}
 
	for(int i = 0; i <= r; i++) {
		dp[i] = -1e9;
	}
	dp[r] = ans;
	for(int i = 0; i < neg.size(); i++) {
		b = neg[i].first;
		a = neg[i].second;
		b -= a;
	// cout<<a<<"--------"<<-b<<endl;
		for(int j = max(a, -b); j <= r; j++){
             // cout<<j<<endl;
			dp[j+b] = max(dp[j+b], dp[j]+1);
		}
	}
	ans = 0;
 
	for(int i = 0; i <= r; i++){
		ans = max(ans, dp[i]);
	}
	cout<<ans<<endl;
	return 0;
}