A - CME
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 50000 + 20;
const int INF = 1000000000;
int main(){
int _;
cin >> _;
while(_--){
int n;
cin >> n;
if(n == 2)puts("2");
else if(n == 3)puts("1");
else cout << n % 2 << endl;
}
}
B - Strings Equalization
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 50000 + 20;
const int INF = 1000000000;
int vis[30];
string s0,s1;
int solve(){
for (int i = 0; i < s0.length(); ++i) {
vis[s0[i]-'a']=1;
}
for (int j = 0; j < s1.length(); ++j) {
if(vis[s1[j]-'a']==1)return true;
}
return false;
}
int main(){
int _;
cin >> _;
while(_--){
memset(vis,0, sizeof(vis));
cin >> s0 >> s1;
if(solve())puts("YES");
else puts("No");
}
}
C - Save the Nature
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 200000 + 20;
const int INF = 1000000000;
ll fare[MAXN];
int precent[MAXN],pre[MAXN];
int n,x,y,a,b;
ll k;
bool greedy(int top)
{
multisetse;
for (int i = 0; i <= top; ++i) {
if(precent[i])se.insert(precent[i]);
}
set::reverse_iterator rit;
int cnt;
ll ans=0;
for (cnt=0,rit = se.rbegin(); rit != se.rend(); rit++,cnt++){
ans += (*rit)*fare[cnt];
//cout << (*rit) << " (*rit) " << endl;
//cout << fare[cnt] << " fare[cnt] " << endl;
}
//cout << "ans:" << ans << endl;
if(ans >= k)return true;
return false;
}
int binary(int l,int r)
{
while(l < r)
{
int mid = (l + r) / 2;
if(greedy(mid))r = mid;
else l = mid + 1;
//cout << "l:" << l << endl;
}
if(greedy(l))return l+1;
return -1;
}
int main(){
int _;
cin >> _;
while(_--){
cin >> n;
memset(precent,0, sizeof(precent));
for (int i = 0; i < n; ++i) {
cin >> fare[i];
fare[i]/=100;
}
sort(fare, fare + n, greater());
cin >> x >> a >> y >> b;
cin >> k;
for (int j = 1; j <= n; ++j) {
if(j % a == 0)precent[j-1]=x;
if(j % b == 0)precent[j-1]+=y;
}
int ans = binary(0,n-1);
cout << ans << endl;
}
}
D - Sequence Sorting
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 300000 + 20;
const int INF = 1000000000;
int n,num[MAXN],nex[MAXN],small[MAXN],big[MAXN],dp[MAXN];
int main(){
int _;
cin >> _;
while(_--){
cin >> n;
memset(small,0x3f, sizeof(int)*(n+1));
memset(big,0, sizeof(int)*(n+1));
memset(nex,-1, sizeof(int)*(n+1));
for (int i = 0; i < n; ++i){
cin >> num[i];
if(small[num[i]] > i)small[num[i]] = i;
if(big[num[i]] < i)big[num[i]] = i;
}
sort(num,num+n);
int allnum=1;
for (int i = 0; i < n-1; ++i){
if(num[i]!=num[i+1]){
nex[num[i]]=num[i+1];
allnum++;
}
}
//cout << "allnum" << allnum << endl;
int maxn = 1;
dp[num[0]]=1;
for (int i = nex[num[0]],j=num[0]; i != -1; i=nex[i],j=nex[j]) {
if(big[j] < small[i])dp[i]=dp[j]+1;
else dp[i]=1;
maxn=max(dp[i],maxn);
}
cout << allnum-maxn << endl;
}
}
E - Paint the Tree
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXN = 500000 + 20;
const int INF = 1000000000;
int k,vis[MAXN];
ll dp[MAXN][2];
struct Edge{
int v,w;
};
vectorvec[MAXN];
vectorarr[MAXN];
ll dfs(int u){
vis[u]=true;
multisetse;
ll occupy=0;
for (int i = 0; i < vec[u].size(); ++i) {
int v=vec[u][i].v,w=vec[u][i].w;
if(vis[v])continue;
//cout << "u v w:" << u << " " << v << endl;
dfs(v);
occupy+=dp[v][1];
//cout << "dp ,w " << dp[v][0] << w << dp[v][1] << endl;
se.insert(dp[v][0]+w-dp[v][1]);
}
set::reverse_iterator rit;
ll add=0;
int cnt=k;
for (rit = se.rbegin(); rit != se.rend() && cnt > 0; rit++,cnt--){
if(cnt==1)dp[u][0]=occupy+add;
add += max((*rit),0);
//cout << (*rit) << " (*rit) " << endl;
//cout << fare[cnt] << " fare[cnt] " << endl;
}
if(se.size()> _;
while(_--){
cin >> n >> k;
for (int i = 0; i <= n; ++i){
vec[i].clear();
arr[i].clear();
}
memset(dp,0,sizeof(ll)*2*(n+1));
memset(vis,0,sizeof(int)*(n+1));
for (int i = 0; i < n - 1; ++i){
int u,v,w;
//cin >> u >> v >> w;
scanf("%d%d%d",&u,&v,&w);
vec[u].push_back(Edge{v,w});
vec[v].push_back(Edge{u,w});
}
cout << dfs(1) << endl;
}
}
F - Stack Exterminable Arrays
//#include
//#include
//#include
//#include
//#include
//#include
//#include
//#include