首页 > 其他分享 >CF1765J Hero to Zero

CF1765J Hero to Zero

时间:2022-12-17 16:11:29浏览次数:62  
标签:Hero int ll db long Zero CF1765J using define

题面传送门

对线性规划这一套还是不熟练。

首先我们发现单点只能减不能加,因此肯定是先变成一个局面然后剩下全部单点修改。因此想要让单点的尽量少,也就是整行/列修改的尽量多。

设\(x_i\)表示第\(i\)行能减的次数,\(y_j\)表示第\(j\)列能减的次数,对于每个\(i,j\)要满足\(x_i+y_j\leq c_{i,j}\),并且要让\(\sum\limits_{i=1}^{n}{x_i+y_i}\)最大。

发现这个东西和KM算法要求的东西极其相似,只是不等号方向反了一反。因此就是最小权完美匹配。

这个显然是\(a,b\)都排序以后按位匹配即可。

时间复杂度\(O(n\log n)\),瓶颈在排序。

code:

#include<bits/stdc++.h>
#define Gc() getchar() 
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n))
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define PB push_back
using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned ll;
using namespace std;const int N=3e5+5,M=10+5,K=1e5+5,mod=1e9+7,Mod=mod-1,INF=2e9+7;const db eps=1e-5;mt19937 rnd(263082);
int n,m,k,x,y,z,A[N],B[N];ll Ans,Ts,Q[N];
int main(){
	freopen("1.in","r",stdin);
	int i,j;scanf("%d",&n);for(i=1;i<=n;i++) scanf("%d",&A[i]);for(i=1;i<=n;i++) scanf("%d",&B[i]);
	sort(A+1,A+n+1);sort(B+1,B+n+1);for(i=1;i<=n;i++) Q[i]=Q[i-1]+B[i];for(i=1;i<=n;i++){
		int p=LB(B+1,B+n+1,A[i])-B-1;Ans+=1ll*p*A[i]-Q[p]+Q[n]-Q[p]-1ll*(n-p)*A[i]; 
	}for(i=1;i<=n;i++) Ts+=abs(A[i]-B[i]);printf("%lld\n",Ans-Ts*(n-1));
}

标签:Hero,int,ll,db,long,Zero,CF1765J,using,define
From: https://www.cnblogs.com/275307894a/p/16989093.html

相关文章

  • opencl优化-Zero Copy
    转自:https://www.cnblogs.com/willhua/tag/OpenCL/有两种方式实现从主机到CL设备的数据传递,第一种:1cl_meminput=clCreateBuffer(context,CL_MEM_READ_ONLY,sizeof(f......
  • 置0函数memset/bzero性能分析
    目录​​memset​​​​实现​​​​bzero​​​​实现​​​​memset和bzero对比​​​​参考​​memset实现bzero实现memset和bzero对比参考https://www.jb51.net/art......
  • Leela Chess Zero
    LeelaChessZero-ChessprogrammingwikiLeelaChessZeroisinitiatedandannouncedbyStockfishco-authorGaryLinscott.LeelaChessisopensource.Thegoal......
  • matlab/simulink中如何使用ones/zeros(变量,变量)不报错
    1.脚本声明变量%使用脚本声明结构体变量m并创建simulink.busclcclearm.a1=[333];busInfo=Simulink.Bus.createObject(m);2.在simulink中使用ones报......
  • mysql中的zeroDateTimeBehavior=convertToNull
    Cannotconvertvalue'0000-00-0000:00:00'fromcolumn1toTIMESTAMP在Mysql数据库中使用DATETIME类型来存储时间,使用JDBC中读取这个字段的时候......
  • lintcode:Trailing Zeros
    15:00StartWriteanalgorithmwhichcomputesthenumberoftrailingzerosinnfactorial.Example11!=39916800,sotheoutshouldbe2ChallengeO(logN)time......
  • [ZMQ] ZeroMQ rk3308 性能测试
    目录stepLatencyTest(one-waylatency)tcpinprocThroughputTesttcpinprocstepadbpush所有附件到udisk目录adbpush的文件添加执行权限LatencyTest(one-waylaten......
  • 283. Move Zeroes
    Givenanarray nums,writeafunctiontomoveall 0'stotheendofitwhilemaintainingtherelativeorderofthenon-zeroelements.Forexample,given num......
  • [LeetCode] 2225. Find Players With Zero or One Losses
    Youaregivenanintegerarray matches where matches[i]=[winneri,loseri] indicatesthattheplayer winneri defeatedplayer loseri inamatch.Return......
  • 通过Mono 在 Heroku 上运行 .NET 应用
    英文原文:​​Running.NETonHeroku​​中文原文:​​在Heroku上运行.NET应用​​自从加入了Heroku之后,我就想在这个平台上运行.NET程序。现在我很高兴向大家宣布,我......