AtCoder Beginner Contest 440 部分题目题解
比赛传送门:AtCoder Beginner Contest 440。
打了一坨史,写完 ABD 然后 E 写红温了然后摆了。
C 题解
可以容易发现,每个位置模 \(2w\) 余数相同的一定会一起被涂到,然后题目转化为环上所有长度为 \(w\) 的连续段的和的最小值。
前缀和随便做一下即可。
#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
inline int read(){
char c=getchar();
int f=1,ans=0;
while(c<48||c>57) f=(c==45?f=-1:1),c=getchar();
while(c>=48&&c<=57) ans=(ans<<1)+(ans<<3)+(c^48),c=getchar();
return ans*f;
}
const int N=4e5+10;
int n,w,c,a[N];
inline void solve(){
n=read(),w=read();
for (int i=0;i<=4*w;i++) a[i]=0;
for (int i=1;i<=n;i++) a[i%(2*w)]+=read();
for (int i=0;i<2*w;i++) a[i+w*2]=a[i];
int ans=1e18;
for (int i=0;i<4*w;i++) a[i]+=a[i-1];
for (int i=0;i+w-1<4*w;i++) ans=min(ans,a[i+w-1]-a[i-1]);
printf("%lld\n",ans);
}
main(){
int T=read();
while(T--) solve();
return 0;
}
E 题解
首先最优的情况为 \((k,0,0,\cdots,0)\),作为初始状态。
假设有一种方案 \((c_1,c_2,\cdots,c_n)\),那么我们有若干种种调整方案形如 \((c_1,c_2,\cdots,c_i-1,c_{i+1}+1,\cdots,c_{n-1},c_n)\),使得比前面的方案小而且没有差太多,利用优先队列 bfs 即可,可以使用 hash 判重,由于每次最多加 \(O(n)\) 个数,复杂度 \(nx\log{nx}\)。
但好像单哈希有点被卡,我瞎调了一下过了,懒得双哈希。
#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int N=55,mod=484508954083;
int n,k,x;unordered_map<int,int>mp;
int a[N];
struct node{
int w,a[N];
inline int hash(){
int ans=0;
for (int i=1;i<=n;i++) ans=(ans*342423%mod+a[i])%mod;
return ans;
}
bool operator <(const node &x) const{
return w<x.w;
}
};
priority_queue<node>q;
inline int read(){
char c=getchar();
int f=1,ans=0;
while(c<48||c>57) f=(c==45?f=-1:1),c=getchar();
while(c>=48&&c<=57) ans=(ans<<1)+(ans<<3)+(c^48),c=getchar();
return ans*f;
}
main(){
n=read(),k=read(),x=read();
for (int i=1;i<=n;i++) a[i]=read();
sort(a+1,a+n+1,greater<int>());
node tmp;tmp.w=a[1]*k,tmp.a[1]=k;
for (int i=2;i<=n;i++) tmp.a[i]=0;
mp[tmp.hash()]=1;
q.push(tmp);
while(x--){
node tmp=q.top();q.pop();
cout <<tmp.w<<endl;
for (int i=1;i<n;i++) if (tmp.a[i]){
tmp.a[i]--;
tmp.a[i+1]++;
tmp.w-=a[i]-a[i+1];
if (!mp.count(tmp.hash())) q.push(tmp),mp[tmp.hash()]=1;
tmp.a[i]++;
tmp.a[i+1]--;
tmp.w+=a[i]-a[i+1];
}
}
return 0;
}

浙公网安备 33010602011771号