http://codeforces.com/contest/802/problem/N

【题解】

方法一:

 #include<bits/stdc++.h>
using namespace std; typedef long long LL; #define N 200020 int to[N], head[N], nxt[N], cost[N], cap[N], cnt; void init(){ memset(head, -, sizeof head); } void add_Edge(int S, int T, int c, int w){
nxt[cnt] = head[S], cap[cnt] = c, cost[cnt] = w, to[cnt] = T, head[S] = cnt ++;
nxt[cnt] = head[T], cap[cnt] = , cost[cnt] = -w, to[cnt] = S, head[T] = cnt ++;
} const LL INF = 1e14; LL dist[N];
int prv[N];
bool vis[N]; LL SPFA(int S, int T, int vet){
queue <int> Q;
fill(prv, prv + vet, -);
fill(dist, dist + vet, INF);
Q.push(S); dist[S] = , vis[S] = true;
while(!Q.empty()){
int x = Q.front(); Q.pop(), vis[x] = false;
for(int id = head[x]; ~id; id = nxt[id]) if( cap[id] ){
int y = to[id];
if(dist[y] > dist[x] + cost[id]){
dist[y] = dist[x] + cost[id];
prv[y] = id;
if(!vis[y]) Q.push(y), vis[y] = true;
}
}
}
if(!~prv[T]) { return INF; } for(int cur = T; cur != S; cur = to[cur^]){
cur = prv[cur];
cap[cur] --;
cap[cur ^ ] ++;
}
return dist[T];
} int a[N], b[N], n, k; int main(){
//freopen("in.txt", "r", stdin);
scanf("%d %d", &n, &k);
for(int i = ; i <= n; i ++) scanf("%d", a + i);
for(int i = ; i <= n; i ++) scanf("%d", b + i); int S = , T = * n + ;
init();
for(int i = ; i <= n; i ++){
add_Edge(S, i, , a[i]);
add_Edge(i + n, T, , b[i]);
add_Edge(i, i + n, n, );
if(i < n) add_Edge(i + n, i + n + , n - i, );
}
LL ans = ;
for(int i = ; i <= k; i ++) ans += SPFA(S, T, T + );
cout << ans << endl;
}

方法二:

 #include <bits/stdc++.h>
using namespace std; #define oo 0x3f3f3f3fLL
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SUM(x,y) accumulate(x,y,0)
#define gcd(x,y) (__gcd(x,y))
#define LOWBIT(x) (x&(-x))
#define CLR(x,y) memset(x,y,sizeof(x))
#define PD(x) printf("%d\n",(x))
#define PF(f) printf("%lf\n",(f))
#define PS(s) puts(s) typedef pair<int, int> pii;
typedef long long LL; template <class T> inline bool chkmin(T& x, T y) { return x > y ? x = y, true : false; }
template <class T> inline bool chkmax(T& x, T y) { return x < y ? x = y, true : false; } template <class T> T reads() {
T x = ;
static int f;
static char c;
for (f = ; !isdigit(c = getchar()); ) if (c == '-') f = -;
for (x = ; isdigit(c); c = getchar()) x = x * + c - ;
return x * f;
}
#define read reads<int>
#define readll reads<LL> int n, k, v, a[], b[];
LL l, r, res, m, ans; int main(void) {
scanf("%d%d", &n, &k);
for (int i = ; i < n; ++i) a[i] = read();
for (int i = ; i < n; ++i) b[i] = read();
for(int i=;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
for(int i=;i<n;i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
l = ; r = 1e10; res = 1e18;
while (l < r) {
ans = ; m = (l + r) >> ; v = ;
priority_queue<LL> qa, qb; for (int i = ; i < n; ++i) {
qa.push(-a[i]); LL ga = -qa.top() + b[i] - m;
LL gb = qb.empty() ? LL(1e18) : b[i] - qb.top();
if (ga <= gb && ga <= ) {
ans += ga; v++;
qa.pop(); qb.push(b[i]);
} else if (ga > gb && gb < ) {
ans += gb;
qb.pop(); qb.push(b[i]);
}
} if (v >= k) { r = m - ; res = ans + k * m; }
else l = m + ;
}
return cout << res << endl, ;
}

最新文章

  1. iOS代码签名理解
  2. ehcache报错
  3. 65行 JavaScript 代码实现 Flappy Bird 游戏
  4. DataSet key points
  5. JFrame背景
  6. angular的注入实现
  7. leetcode_question_112 Path Sum
  8. log4j.properties 的使用详解
  9. Vue.js 组件笔记
  10. Servlet中文乱码处理
  11. C#控制台程序使用Log4net日志组件
  12. ROS机器人程序设计-学习小结-
  13. 集合框架之map
  14. Mac更改PHP默认目录
  15. windows 同时启动多个Tomcat 控制台日志(startup.bat)输出到指定文件中
  16. 李洪强iOS开发之-FMDB的用法
  17. HTML&amp;CSS 问题
  18. 北京Uber优步司机奖励政策(12月23日)
  19. Mac brew安装redis
  20. windows 架设SVN服务器

热门文章

  1. 前端的百度地图的api的使用
  2. 洛谷P1251 餐巾计划问题(最小费用最大流)
  3. Java入门小知识
  4. ARM 环境下使用azure powershell 从远程blob中拉去vhd 并创建虚拟机
  5. 初试springWebMVC
  6. vue-devtools在google浏览器下安装扩展
  7. 原创:PHP编译安装配置参数说明
  8. hdu5792 World is Exploding(多校第五场)树状数组求逆序对 离散化
  9. 1-2 编程基础 GDB程序调试
  10. BZOJ2120 数颜色(树套树)