Codeforces Round #426 (Div. 1) (ABCDE)

1. 833A The Meaningless Game

大意: 初始分数为$1$, 每轮选一个$k$, 赢的人乘$k^2$, 输的人乘$k$, 给定最终分数, 求判断是否成立.

判断一下$a\cdot b$是否是立方数, 以及能否被那个立方的因子整除即可. cbrt竟然有误差, 特判了一下, 好坑

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head int main() {
int n;
scanf("%d", &n);
while (n--) {
int a, b;
scanf("%d%d", &a, &b);
ll x = (ll)a*b, t = cbrt(x);
while (t*t*t>x) --t;
while (t*t*t<x) ++t;
puts(t*t*t==x&&a%t==&&b%t==?"Yes":"No");
}
}

2. 833B The Bakery

大意: 给定$n$元素序列, 求划分为最多$x$段, 每段的贡献为不同元素数, 求最大贡献.

设$dp_{i,j}$为前$i$个数分成$j$段的最大值, 开$k$棵线段树, 位置$x$维护dp[x-1][j]+[x...i]的贡献即可

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head const int N = ;
int n,k,a[N],pre[N],vis[N];
int dp[N][];
//dp[i][j] = 前i个数,分成j段的最大值
//要支持区间加, 查询区间最值
struct {
int ma[N<<],tag[N<<];
void pd(int o) {
if (tag[o]) {
ma[lc]+=tag[o],tag[lc]+=tag[o];
ma[rc]+=tag[o],tag[rc]+=tag[o];
tag[o]=;
}
}
void add(int o, int l, int r, int ql, int qr, int v) {
if (ql<=l&&r<=qr) return ma[o]+=v,tag[o]+=v,void();
pd(o);
if (mid>=ql) add(ls,ql,qr,v);
if (mid<qr) add(rs,ql,qr,v);
ma[o]=max(ma[lc],ma[rc]);
}
} tr[]; int main() {
scanf("%d%d", &n, &k);
REP(i,,n) {
scanf("%d",a+i);
pre[i] = vis[a[i]];
vis[a[i]] = i;
}
REP(i,,n) {
REP(j,,k-) tr[j].add(,,n,pre[i]+,i,);
REP(j,,k) dp[i][j] = tr[j-].ma[];
if (i==n) break;
REP(j,,k-) tr[j].add(,,n,i+,i+,dp[i][j]);
}
printf("%d\n", dp[n][k]);
}

3. 833C Ever-Hungry Krakozyabra

大意: 给定$L,R$, 将区间$[L,R]$的每个数去除数位$0$后对数位进行排序, 求一共能得到多少种数.

先特判掉1e18, 所有可能得到的数最多为$\binom{18+9}{9}=1562275$, 可以直接暴力枚举每个数$x$, 判断$x$是否能在区间$[L,R]$得到即可. 判断合法性本来写的$O(18^2)$的暴力贪心, 结果TLE on test 70. 按照官方题解改成复杂度$O(10*18)$的$dfs$就过了...... 看评论区说似乎有不用暴力枚举的做法, 没太懂

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head int ans,len,flag,cnt,a[],x[],y[];
ll L, R, fac[]; int solve(int d, int l1, int l2) {
if (!l1&&!l2||d<) return ;
int U=l1?x[d]:,D=l2?y[d]:;
REP(i,U,D) if (a[i]) {
--a[i];
if (solve(d-,l1&&x[d]==i,l2&&y[d]==i)) return ++a[i],;
++a[i];
}
return ;
} int chk() {
return solve(len-,,);
} //生成所有合法的数
void dfs(int d, int pre) {
if (d>len) return;
REP(i,pre,) {
++a[i],++cnt;
a[] = len-cnt;
ans += chk();
dfs(d+,i);
--a[i],--cnt;
}
} int main() {
fac[] = ;
REP(i,,) fac[i] = fac[i-]*;
scanf("%lld%lld",&L,&R);
if (R==fac[]) --R, flag = ;
if (L==fac[]) --L;
ll t = L;
while (t) x[len++]=t%,t/=;
t = R, len = ;
while (t) y[len++]=t%,t/=;
dfs(,);
printf("%d\n",ans);
}

4.

5. 833E Caramel Clouds

大意: 给定$n$朵云, 第$i$朵出现时间范围$[l_i,r_i]$, 删除需要花费$c_i$, 初始时刻为$0$, 预算为$C$. 给定$m$个询问$k_i$, 求一棵需要照$k_i$时间阳光的花最快什么时间长成, 最多删除两朵云, 询问独立.

参考的这个https://www.cnblogs.com/ECJTUACM-873284962/p/7265224.html

主要思路就是依次处理每个时间段, 预处理每个时刻的最大光照时间.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=%P;for (a%=P;n;a=a*a%P,n>>=)if(n&)r=r*a%P;return r;}
ll inv(ll x){return x<=?:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=;char p=getchar();while(p<''||p>'')p=getchar();while(p>=''&&p<='')x=x*+p-'',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+;
#else
const int N = 1e2+;
#endif int n, C, cost[N], tag[N], b[N];
pii tr1[N],tr2[N]; //树状数组维护区间最大次大
void add(int id, int x, int v) {
x = lower_bound(b+,b++*b,x)-b;
for (; x<=*b; x+=x&-x) {
if (tr1[x].y==id) tr1[x].x = max(tr1[x].x,v);
else if (tr2[x].x<v) tr2[x] = pii(v,id);
if (tr1[x]<tr2[x]) swap(tr1[x],tr2[x]);
}
}
int qry(int id, int x) {
x = upper_bound(b+,b++*b,x)-b-;
int ret = ;
for (; x; x^=x&-x) {
if (id==tr1[x].y) ret=max(ret,tr2[x].x);
else ret=max(ret,tr1[x].x);
}
return ret;
} int main() {
scanf("%d%d", &n, &C);
vector<pii> events;
REP(i,,n) {
int l,r;
scanf("%d%d%d",&l,&r,cost+i);
events.pb(pii(l,i));
events.pb(pii(r,i));
b[i] = cost[i];
}
events.pb(pii(,));
events.pb(pii(2e9,));
sort(events.begin(),events.end());
sort(b+,b++n),*b=unique(b+,b++n)-b-;
REP(i,,*b+) tr1[i].y=tr2[i].y=-;
vector<pii> ans;
map<pii,int> len;
set<int> s;
int mx = ;
if (events[].y) s.insert(events[].y);
for (int i=; i<events.size(); ++i) {
int d = events[i].x-events[i-].x;
if (d>&&s.size()<=) {
int p = , q = ;
if (s.size()) p = *s.begin();
if (s.size()==) q = *s.rbegin();
int start = -;
if (!p) {
start = mx;
len[{,}] += d;
}
else if (!q) {
if (cost[p]<=C) {
start = len[{p,}]+len[{,}];
//找一个q, 使得len[{p,q}]+len[{q,0}]的最大
int ma = tag[p]; //相交的情况
//不相交的情况
ma = max(ma, qry(p,C-cost[p]));
start += ma;
len[{p,}] += d;
add(p,cost[p],len[{p,}]);
}
}
else if (cost[p]+cost[q]<=C) {
start = len[{p,q}]+len[{p,}]+len[{q,}]+len[{,}];
len[{p,q}] += d;
tag[p] = max(tag[p], len[{p,q}]+len[{q,}]);
tag[q] = max(tag[q], len[{p,q}]+len[{p,}]);
}
if (~start&&start+d>mx) {
mx = start+d;
ans.pb(pii(mx, events[i].x));
}
}
int id = events[i].y;
if (!id) continue;
if (s.count(id)) s.erase(id);
else s.insert(id);
}
int m;
scanf("%d", &m);
while (m--) {
int k;
scanf("%d", &k);
auto p = lower_bound(ans.begin(),ans.end(),pii(k,));
printf("%d\n", p->y-(p->x-k));
}
}
上一篇:pycharm企业版注册码


下一篇:Codeforces.24D.Broken robot(期望DP 高斯消元)