HDU 6364 Ringland
题意: 在一个环上有n个男生, n个女生, 现在要求每一个男生与女生配对, 求总代价最小。
题解:
如果2个男生到女生的路交叉了, 那么我们交换这2个男生的路, 总代价是一定会变得小的。
所以组合方式是线性的.
假设有3个男生 3个女生, 并且都已经排好序了。
搭配方式一共有3种。
1 <-> 1 2 <-> 2 3 <-> 3
1 <-> 2 2 <-> 3 3 <-> 1
1 <-> 3 2 <-> 1 3 <-> 2
因为不想要路径交叉, 所以就按照顺序给他们排序配对。
接下来就是顺时针走路和逆时针走的问题了。
先将B 扩展成 3B 减少讨论。
然后对于 a 来说 找到 a - 2/L 的位置 和 a + 1 的位置,
对 [a - 2/L, a] 来说 贡献都是 a-b, 对 [a + 1, a + 2/ L] 贡献都是 b-a.
然后对于每一个B也找到进入计算的时间, 出去的时间, 从负变成正的时间,然后搞一下差分就好了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = (int)1e9+;
const int N = 2e6;
LL a[N], b[N];
int l[N], r[N], v[N], in[N], out[N];
LL sum[N];
struct FastIO {
static const int S = ;
int wpos;
char wbuf[S];
FastIO() : wpos() { }
inline int xchar() {
static char buf[S];
static int len = , pos = ;
if (pos == len) pos = , len = fread(buf, , S, stdin);
if (pos == len) return -;
return buf[pos++];
}
inline int xint() {
int c = xchar(), x = , s = ;
while (c <= ) c = xchar();
if (c == '-') s = -, c = xchar();
for (; '' <= c && c <= ''; c = xchar()) x = x * + c - '';
return x * s;
}
~FastIO() {
if (wpos) fwrite(wbuf, , wpos, stdout), wpos = ;
}
} io;
inline int Find(int x, LL v){
while(b[x] < v) x++;
return x;
}
LL n, L;
void in1(){
n = io.xint();
L = io.xint();
for(int i = ; i <= n; i++){
a[i] = io.xint();
a[i] += L;
}
for(int i = ; i <= n; i++)
b[i] = io.xint();
}
void in2(){
scanf("%lld%lld", &n, &L);
for(int i = ; i <= n; i++){
scanf("%lld", &a[i]);
a[i] += L;
}
for(int i = ; i <= n; i++)
scanf("%lld", &b[i]);
}
void solve(){
in1();
//in2();
for(int i = ;i <= *n; i++)
b[i+n] = b[i] + L;
for(int i = ; i <= n; i++){
l[i] = Find(l[i-], a[i] - L/);
r[i] = Find(r[i-], a[i] + );
}
int lf = l[];
memset(sum, , sizeof(LL) *(n+));
for(int i = ; i <= n; i++) sum[] += a[i];
for(int i = ; i <= n; i++){
int t = r[i] - lf + ;
if(t - i >= ) sum[t-i+] -= * a[i];
else sum[] -= a[i];
}
v[] = ;
out[] = ;
int j = ;
for(int i = lf; i <= n*; i++, j++){
if(j <= n) in[j] = ;
else in[j] = in[j-] + ;
out[j] = out[j-] + ;
v[j] = v[j-];
while(v[j] <= n && a[v[j]] < b[i]) v[j]++;
if(in[j] > n) break;
}
for(int i = ; i < j; i++){
if(i <= n){
v[i] = i - (v[i]-) + ;
if(v[i] < ) v[i] = ;
}
else {
if(v[i] == n+) v[i] = in[i];
else v[i] = in[i] + (n - v[i] + );
}
}
for(int i = , p = lf; i < j; i++, p++){
sum[in[i]] -= b[p];
if(out[i] <= v[i]) sum[out[i]] += b[p];
else {
sum[v[i]] += * b[p];
sum[out[i]] -= b[p];
}
}
LL ans = INF, tmp = ;;
for(int i = ; i <= n; i++){
tmp += sum[i];
ans = min(ans, tmp);
}
printf("%lld\n", ans);
}
int main(){
int t;
scanf("%d", &t);
while(t--){
solve();
}
return ;
}
HDU 6364 Ringland的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- 【Intellij IDEA】设置 jdk 版本
File -> Project Structure... -> Project,如图所示:
- hdoj 4706 Children's Day
题目意思就是用a-z组成一个N,然后到z后又跳回a,输出宽从3到10的N. #include <stdio.h> #include <string.h> char s[14][ ...
- asp.net core系列 70 即时通迅-WebSocket+Redis发布订阅
一.概述 在asp.net core 中可以用WebSocket 或asp.net core SignalR来开发即时通迅.在项目中由于开发前后端分离,对于SignalR前端技术人员不想依赖juqer ...
- CodeGlance右侧窗口缩略图消失不见
说明下问题,idea中的CodeGlance插件会在右侧显示缩略图,可以快速定位代码.今天遇到个问题升级了插件后右侧窗口消失.经过卸载插件,重启,reset一系列操作后还是没能恢复. 能去搜索引擎搜索 ...
- .xxx.sh脚本无法启动,原来都是特殊字符搞的鬼?
今天遇到个趣的问题,linux上springboot启动,连接达梦数据库报错. 解决思路: 1)是不是数据库本身有问题,客户端登录没问题. 2)排查是不是war包问题,本地连接数据库,没问题. 3)是 ...
- hadoop学习(三)----hadoop2.x完全分布式环境搭建
今天我们来完成hadoop2.x的完全分布式环境搭建,话说学习本来是一件很快乐的事情,可是一到了搭环境就怎么都让人快乐不起来啊,搭环境的时间比学习的时间还多.都是泪.话不多说,走起. 1 准备工作 开 ...
- ccf 201903-3 损坏的RAID5
9月份考ccf,暑假打算做一些往年的真题... 这个题,一开始真是把我给看晕了 传说中的大模拟,果然不简单QAQ 首先读懂题目就是一个大难点,特别是对于我这种题目一长就看不进去的人来说 读懂题目之后, ...
- Linux故障处理最佳实践
引言 业务中断了! 老板咆哮,主管抓狂,而你就是那个要去处理故障.恢复业务的不幸的人. 你独自一人在阴暗的隔间里.北边是老板的办公室,西边是Team Leader的办公室,南面是茶水间,在那你能泡上一 ...
- Codeforces 436D Pudding Monsters
题意简述 开始有无限长的一段格子,有n个格子种有布丁怪兽,一开始连续的布丁怪兽算一个布丁怪兽. 每回合你可以将一个布丁怪兽向左或右移动,他会在碰到第一个布丁怪兽时停下,并与其合并. 有m个特殊格子,询 ...
- React SPA 应用 hash 路由如何使用锚点
当我们在做 SPA 应用的时候,为了兼容老的浏览器(如IE9)我们不得不放弃 HTML5 browser history api 而只能采用 hash 路由的这种形式来实现前端路由,但是因为 hash ...