题解:CF2068K Amusement Park Rides
朴素的思路就是将每个
发现我们加入的那些代表时间的点很多都是没有用的,如果找到一种方法使得加入的右部点的数量在可接受的范围内,就可以通过。
先使用一个桶来统计
接下来使用一个 std::map<ll,std::set<int> > 来维护要加入的右部点的值,以及这个右部点所可以连接的左部点。具体方法如下:
- 初始时对于
\forall i\in[1,k] ,将(p_i,\{i\}) 插入std::map中,代表值为p_i 的右部点可以与左部点i 相连。 - 然后重复如下过程直到流量为
n : - 取出
std::map中key最小的键值对,记为(x,s_x) 并且用x 来更新答案。 - 新建一个右部点
v_x 代表x ,并将v_x 和T ,集合s_x 中的左部点和v_x 都连上流量为1 的边。 - 尝试扩展最大流,如果加入
v_x 之后最大流增大了,就把\forall y\in s_x 插入到s_{x+p_y} 中。 - 最后删除键值对
(x,s_x) 。
像这样操作,加入的右部点的数量就是
参考实现:
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define il inline
#define ll long long
#define lll __int128
#define ull unsigned long long
#define ld long double
#define db double
#define gc getchar();
#define pc(x) putchar(x)
#define O(x) cout<<x<<'\n';
#define adde(x,y) emplace_back(make_pair(x,y))
#define ep emplace
#define eb emplace_back
#define mp make_pair
#define pbset(typ) tree< typ ,null_type,std::less< typ >, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>
#define pbmap(typ1,typ2) tree< typ1 , typ2 ,std::less< typ1 >, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>
namespace constant_warrior{
template<typename T> inline void fr(T& num){
num=0;short sign=1;char ch=std::getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')sign=-1;
ch=std::getchar();
}
while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
num=num*sign;
}
template<typename T>inline void fw(T x){
if(x<0)std::putchar('-'),x=-x;
if(x>9)fw(x/10);
std::putchar(x%10+'0');
}
template<typename T>inline const T& maxf(const T& a,const T& b){
if(a>b)return a;
return b;
}
template<typename T>inline const T& minf(const T& a,const T& b){
if(a>b)return b;
return a;
}
template<typename T>inline void swapf(T& a,T& b){
a^=b^=a^=b;
}
template<typename T, typename... Args> inline void fr(T &x, Args&... args) {
fr(x);
fr(args...);
}
}
using namespace constant_warrior;
const int N=1<<14,M=1048576,K=1024,inf=0x3f3f3f3f;
int n,a[N],s,t;
map<int,int> tong;
int p[N],q[N],tot,vc;
struct edge{
int to,wt,nxt;
}e[M];
int head[N],d[N],now[N],cnt=1;
inline void add(int x,int y,int z){
e[++cnt]=(edge){y,z,head[x]},head[x]=cnt;
e[++cnt]=(edge){x,0,head[y]},head[y]=cnt;
}
inline bool bfs(){
queue<int> q;
memset(d,0,sizeof d);
q.emplace(s);
d[s]=1,now[s]=head[s];
while(q.size()){
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].nxt){
if(e[i].wt&&!d[e[i].to]){
q.emplace(e[i].to);
now[e[i].to]=head[e[i].to];
d[e[i].to]=d[x]+1;
if(e[i].to==t)return 1;
}
}
}
return 0;
}
inline int dinic(int x,int flow){
if(x==t)return flow;
int rest=flow,k,i;
for(i=now[x];i&&rest;i=e[i].nxt){
now[x]=i;
if(e[i].wt&&d[e[i].to]==d[x]+1){
k=dinic(e[i].to,min(rest,e[i].wt));
if(!k)d[e[i].to]=0;
e[i].wt-=k,e[i^1].wt+=k,rest-=k;
}
}
return flow-rest;
}
inline int maxflow(){
int flow=0,res=0;
while(bfs()){
while(flow=dinic(s,inf)){
res+=flow;
}
}
return res;
}
map<ll,set<ll> > ms;
#define mi map<ll,set<ll> >::iterator
void solve(){
fr(n),vc=n;
s=++vc,t=++vc;
for(int i=1;i<=n;i++){
fr(a[i]);
tong[a[i]]++;
}
for(auto [i,j]:tong){
p[++tot]=i;
q[tot]=j;
add(s,tot,q[tot]);
ms[p[tot]].emplace(tot);
}
tong.clear();
int fl=0;
ll res=0;
while(fl<n){
mi i=ms.begin();
// cerr<<i->first<<'\n';
res=max(res,i->first);
add(++vc,t,1);
for(auto j:i->second){
add(j,vc,1);
}
int f=maxflow();
if(f){
fl+=f;
for(auto j:i->second){
ms[i->first+p[j]].emplace(j);
}
}
ms.erase(i);
}
// cerr<<"R: ";
fw(res),pc('\n');
for(int i=1;i<=vc;i++)head[i]=now[i]=0;
ms.clear();
tot=vc=0,cnt=1;
}
int main(){
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
int T=1;cin>>T;
while(T-->0)solve();
}