开题,发现 不会, 不会, 原题, 不会
然后先用了 把 写了,调了 贪心,到结束都不知道那假了
垫底了
A.饥饿的狐狸
不难想到最小值就是顺着吃,可以 出解
而最大值贪心的策略是以 为分界线左右横跳,最后剩都在一边的时候就分类讨论取最大值就行了
时间复杂度
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fir first
#define sec second
#define int long long
using namespace std;
namespace IO{
#define File(x,y) freopen(#x,"r",stdin),freopen(#y,"w",stdout)
#define fill(a,b,c,d) memset(a,b,sizeof(c)*(d+1))
#define fillall(x,y) memset(x,y,sizeof(x))
inline int read(){
int w=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){w=(w<<1)+(w<<3)+(ch^48); ch=getchar();}
return w*f;
}
}
using namespace IO;
namespace CL{
const int maxn=1e5+5;
int n,w;
int a[maxn];
multiset<int> s;
inline int solve(bool flag){
int lst=w,res=0; s.clear();
for(int i=1;i<=n;i++) s.insert(a[i]);
while(s.size()){
int now=flag?*--s.end():*s.begin();
if((flag && now<w) || (!flag && now>w)) break;
s.erase(s.find(now));
res+=abs(lst-now);
lst=now,flag^=1;
}
while(s.size()){
int now=flag?*--s.end():*s.begin();
s.erase(s.find(now));
res+=max(abs(w-now),abs(lst-now));
lst=now,flag^=1;
}
return res;
}
inline int main(){
File(a.in,a.out);
n=read(),w=read();
for(int i=1;i<=n;i++) a[i]=read();
sort(a+1,a+1+n);
printf("%lld %lld\n",max(0ll,w-a[1])+max(0ll,a[n]-w),max(solve(0),solve(1)));
return 0;
}
}
signed main(){return CL::main();}B.保险箱
若密码为 ,则 有:
由裴蜀定理可知,左式的最小整数值为 ,设其为
也就是说要有
又因为 为定值,那么可得 且若 一定那么
那么直接枚举 的因子,判断是否整除 即可
时间复杂度
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fir first
#define sec second
#define int long long
using namespace std;
namespace IO{
#define File(x,y) freopen(#x,"r",stdin),freopen(#y,"w",stdout)
#define fill(a,b,c,d) memset(a,b,sizeof(c)*(d+1))
#define fillall(x,y) memset(x,y,sizeof(x))
inline int read(){
int w=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){w=(w<<1)+(w<<3)+(ch^48); ch=getchar();}
return w*f;
}
}
using namespace IO;
namespace CL{
const int maxn=2.5e5+5;
int n,K,tot;
int a[maxn];
inline int gcd(int a,int b){return !b?a:gcd(b,a%b);}
inline bool check(int x){
for(int i=tot;i>=1;i--){
if(a[i]%x==0) return 0;
if(a[i]<x) return 1;
}
return 1;
}
inline int main(){
File(b.in,b.out);
n=read(),K=read();
for(int i=1;i<=K;i++) a[i]=read();
int g=gcd(n,a[K]);
sort(a+1,a+K);
tot=unique(a+1,a+K)-a-1;
for(int i=1;i*i<=g;i++) if(g%i==0 && check(i)) return printf("%lld\n",n/i),0;
for(int i=sqrt(g);i>=1;i--) if(g%i==0 && check(g/i)) return printf("%lld\n",n/(g/i)),0;
return 0;
}
}
signed main(){return CL::main();}C.追逐
原题,见 「考试总结」2021.7.20
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fir first
#define sec second
#define int long long
using namespace std;
namespace IO{
#define File(x,y) freopen(#x,"r",stdin),freopen(#y,"w",stdout)
#define fill(a,b,c,d) memset(a,b,sizeof(c)*(d+1))
#define fillall(x,y) memset(x,y,sizeof(x))
inline int read(){
int w=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){w=(w<<1)+(w<<3)+(ch^48); ch=getchar();}
return w*f;
}
}
using namespace IO;
namespace CL{
const int maxn=1e5+5,maxm=105;
int n,m,ans;
int val[maxn],sum[maxn],stk[maxn],f[maxn][maxm],g[maxn][maxm];
namespace Graph{
int head[maxn],id;
struct e{int v,next;}edge[maxn<<1];
inline void add(int u,int v){
edge[++id]=(e){v,head[u]};
head[u]=id;
}
}using namespace Graph;
void dfs(int u,int fa){
for(int i=1;i<=m;i++) f[u][i]=sum[u],g[u][i]=sum[u]-val[fa];
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v;
if(v==fa) continue;
dfs(v,u);
for(int j=0;j<=m;j++) ans=max(ans,f[u][j]+g[v][m-j]);
for(int j=1;j<=m;j++){
f[u][j]=max(f[u][j],max(f[v][j],f[v][j-1]+sum[u]-val[v]));
g[u][j]=max(g[u][j],max(g[v][j],g[v][j-1]+sum[u]-val[fa]));
}
}
for(int i=1;i<=m;i++) f[u][i]=sum[u],g[u][i]=sum[u]-val[fa];
int top=0;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].v;
if(v==fa) continue;
stk[++top]=v;
}
while(top){
int v=stk[top--];
for(int j=0;j<=m;j++) ans=max(ans,f[u][j]+g[v][m-j]);
for(int j=1;j<=m;j++){
f[u][j]=max(f[u][j],max(f[v][j],f[v][j-1]+sum[u]-val[v]));
g[u][j]=max(g[u][j],max(g[v][j],g[v][j-1]+sum[u]-val[fa]));
}
}
}
inline int main(){
File(c.in,c.out);
n=read(); m=read();
for(int i=1;i<=n;i++) val[i]=read();
for(int i=1;i<n;i++){
int x=read(),y=read();
add(x,y),add(y,x);
sum[x]+=val[y],sum[y]+=val[x];
}
dfs(1,0);
printf("%lld\n",ans);
return 0;
}
}
signed main(){return CL::main();}D.字符串
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fir first
#define sec second
using namespace std;
namespace IO{
#define File(x,y) freopen(#x,"r",stdin),freopen(#y,"w",stdout)
#define fill(a,b,c,d) memset(a,b,sizeof(c)*(d+1))
#define fillall(x,y) memset(x,y,sizeof(x))
inline int read(){
int w=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){w=(w<<1)+(w<<3)+(ch^48); ch=getchar();}
return w*f;
}
}
using namespace IO;
namespace CL{
const int maxn=5e5+5;
int n,q;
int a[maxn];
char s[maxn];
namespace SegmentTree{
#define lson p<<1
#define rson p<<1|1
struct node{
int sum,maxx,pre,suf;
node(){sum=maxx=pre=suf=0;}
node(int _sum,int _maxx,int _pre,int _suf){sum=_sum,maxx=_maxx,pre=_pre,suf=_suf;}
inline node operator +(const node &x)const{
node res;
res.sum=sum+x.sum;
res.maxx=max(max(maxx,x.maxx),suf+x.pre);
res.pre=max(pre,sum+x.pre);
res.suf=max(x.suf,suf+x.sum);
return res;
}
}t[maxn<<2];
inline void pushup(int p){t[p]=t[lson]+t[rson];}
inline void build(int p,int l,int r){
if(l==r) return t[p]=node(a[l],a[l],a[l],a[l]),void();
int mid=(l+r)>>1;
build(lson,l,mid); build(rson,mid+1,r);
pushup(p);
}
inline node query(int p,int l,int r,int tl,int tr){
if(tl<=l && r<=tr) return t[p];
int mid=(l+r)>>1;
if(tl<=mid && tr>mid) return query(lson,l,mid,tl,tr)+query(rson,mid+1,r,tl,tr);
if(tl<=mid) return query(lson,l,mid,tl,tr);
if(tr>mid) return query(rson,mid+1,r,tl,tr);
}
}using namespace SegmentTree;
inline int main(){
File(d.in,d.out);
n=read(),scanf("%s",s+1),q=read();
for(int i=1;i<=n;i++) a[i]=s[i]=='C'?1:-1;
build(1,1,n);
while(q--){
int l=read(),r=read();
node res=query(1,1,n,l,r);
printf("%d\n",max(0,res.maxx)-res.sum);
}
return 0;
}
}
signed main(){return CL::main();}
