A Array with Odd Sum
如果原先和就是奇数,不管
如果原先和是偶数,就看看能不能把随便一个奇数改成偶数,或者把随便一个偶数改成奇数
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
ll n, a[maxn], c[2];
int main()
{
ll T=read(), i;
while(T--)
{
cl(c);
n=read();
rep(i,1,n)a[i]=read(), c[a[i]&1]++;
ll s=0;
rep(i,1,n)s+=a[i];
if(s&1)
{
printf("YES\n");
}
else
{
if(c[0] and c[1])printf("YES\n");
else printf("NO\n");
}
}
return 0;
}
B Food Buying
每次都支付 10 10 10的倍数,直到最后钱数 < 10 <10 <10,再把剩下的钱一块付了
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
ll T, ans, s;
int main()
{
T=read();
while(T--)
{
s=read();
ans=0;
while(s>=10)
{
ans+=s-s%10;
s=s/10+s%10;
}
printf("%lld\n",ans+s);
}
return 0;
}
C Yet Another Walking Robot
维护两种前缀和, m a p map map瞎搞
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
ll T=read(), n, a[maxn], s1[maxn], s2[maxn];
char s[maxn];
map<pll,ll> tb;
int main()
{
pll ans;
ll i;
while(T--)
{
n=read();
scanf("%s",s+1);
rep(i,1,n)
{
s1[i]=s1[i-1];
s2[i]=s2[i-1];
if(s[i]=='U')s1[i]++;
if(s[i]=='D')s1[i]--;
if(s[i]=='L')s2[i]--;
if(s[i]=='R')s2[i]++;
}
ans=pll(-1,-1);
tb.clear();
tb[pll(0,0)]=0;
rep(i,1,n)
{
if(tb.find(pll(s1[i],s2[i]))!=tb.end())
{
auto pos=tb[pll(s1[i],s2[i])];
if(ans.first==-1 or ans.second-ans.first>i-pos)
ans = pll(pos,i);
}
tb[pll(s1[i],s2[i])]=i;
}
if(ans.first==-1)printf("-1\n");
else printf("%lld %lld\n",ans.first+1,ans.second);
}
return 0;
}
D Fight with Monsters
算出每个怪兽需要消耗的技能点数,然后贪心先选择所需点数少的怪兽使用即可
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
ll n, a, b, k, ans, hp[maxn];
int main()
{
ll i;
n=read(), a=read(), b=read(), k=read();
rep(i,1,n)hp[i]=read();
vector<ll> v;
rep(i,1,n)
{
ll res = hp[i]%(a+b)-a;
if(res==-a)res=b;
if(res<=0)
{
ans++;
}
else
{
v.emb(res/a+!!(res%a));
}
}
sort(v.begin(),v.end());
for(auto x:v)
{
if(x<=k)k-=x, ans++;
}
printf("%lld",ans);
return 0;
}
E1 String Coloring (easy version)
见 E 2 E2 E2
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
vector<char> v[30];
char s[maxn];
ll ans[maxn];
int main()
{
ll tot=0, i, n, j;
n=read(); scanf("%s",s+1);
rep(i,1,n)
{
ll pos=-1;
rep(j,1,tot)
{
if(v[j].back()<=s[i])
{
if(pos==-1)pos=j;
else
{
if(v[j].back()>v[pos].back())pos=j;
}
}
}
if(pos==-1)
{
tot++;
pos=tot;
}
v[pos].emb(s[i]);
ans[i]=pos;
}
if(tot>2)
{
printf("NO\n");
return 0;
}
printf("YES\n");
rep(i,1,n)printf("%lld",ans[i]-1);
return 0;
}
E2 String Coloring (hard version)
一个显然易见的结论就是但不会超过 26 26 26,因为我完全可以对每种字母都开一种颜色,这样正好是 26 26 26种颜色
贪心做即可,每次来一个字母 c c c,就找一种颜色,使得这种颜色的最后一个字母不大于 c c c,且这种颜色的最后一个字母尽量大
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
vector<char> v[30];
char s[maxn];
ll ans[maxn];
int main()
{
ll tot=0, i, n, j;
n=read(); scanf("%s",s+1);
rep(i,1,n)
{
ll pos=-1;
rep(j,1,tot)
{
if(v[j].back()<=s[i])
{
if(pos==-1)pos=j;
else
{
if(v[j].back()>v[pos].back())pos=j;
}
}
}
if(pos==-1)
{
tot++;
pos=tot;
}
v[pos].emb(s[i]);
ans[i]=pos;
}
printf("%lld\n",tot);
rep(i,1,n)printf("%lld ",ans[i]);
return 0;
}
F Berland Beauty
把路径按照边权降序排序,然后依次涂色,如果发现某种颜色涂不了了,就无解
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 5010
#define maxe 10010
#define maxk 15
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
struct Graph
{
int etot, head[maxn], to[maxe], next[maxe], w[maxe];
void clear(int N)
{
for(int i=1;i<=N;i++)head[i]=0;
etot=1;
}
void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
#define forp(pos,G) for(auto p=G.head[pos];p;p=G.next[p])
}G;
struct Doubling_LCA
{
int f[maxn][maxk+1], depth[maxn];
void clear(int n){for(int i=1;i<=n;i++)depth[i]=0, cl(f[i]);}
void dfs(Graph &G, int pos, int pre)
{
for(auto k=1;(1<<k)<=depth[pos];k++)f[pos][k]=f[f[pos][k-1]][k-1];
for(auto p(G.head[pos]);p;p=G.next[p])
if(G.to[p]!=pre)
{
f[G.to[p]][0]=pos;
depth[G.to[p]]=depth[pos]+1;
dfs(G,G.to[p],pos);
}
}
void run(Graph &G, int root)
{
depth[root]=1;
dfs(G,root,0);
}
int q(int x, int y)
{
if(depth[x]<depth[y])swap(x,y);
for(auto k(maxk);~k;k--)
if(depth[f[x][k]]>=depth[y])
x=f[x][k];
if(x==y)return x;
for(auto k(maxk);~k;k--)
if(f[x][k]!=f[y][k])
x=f[x][k], y=f[y][k];
return f[x][0];
}
int jp(int x, int b)
{
for(auto k=0;k<=maxk;k++)
if(b&(1<<k))x=f[x][k];
return x;
}
}db;
ll col[maxn], n, m, mn[maxn], a[maxn], b[maxn], id[maxn], tb[maxn][maxn];
int main()
{
ll i, j, u, v;
n=read();
rep(i,1,n-1)
{
u=read(), v=read();
G.adde(u,v), G.adde(v,u);
tb[u][v]=tb[v][u]=i;
}
m=read();
rep(i,1,m)
{
id[i]=i;
a[i]=read(), b[i]=read(), mn[i]=read();
}
sort(id+1,id+m+1,[](ll x, ll y){return mn[x]>mn[y];});
db.run(G,1);
rep(i,1,m)
{
ll u=a[id[i]], v=b[id[i]], lca=db.q(u,v), cnt=0;
while(u!=lca)
{
ll e=tb[u][db.f[u][0]];
if(!col[e] or col[e]==mn[id[i]])col[e]=mn[id[i]],cnt++;
u=db.f[u][0];
}
while(v!=lca)
{
ll e=tb[v][db.f[v][0]];
if(!col[e] or col[e]==mn[id[i]])col[e]=mn[id[i]],cnt++;
v=db.f[v][0];
}
if(cnt==0)
{
printf("-1");
return 0;
}
}
rep(i,1,n-1)
if(col[i])printf("%lld ",col[i]);
else printf("1000000 ");
return 0;
}