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的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. 你可能不知道的Docker资源限制

    What is 资源限制? 默认情况下,容器是没有资源限制的,它会尽可能地使用宿主机能够分配给它的资源.Docker提供了一种控制分配多少量的内存.CPU或阻塞I/O给一个容器的方式,即通过在dock ...

  2. UE4 坐标系 坐标轴旋转轴

    Pitch是围绕Y轴旋转,也叫做俯仰角. Yaw是围绕Z轴旋转,也叫偏航角. Roll是围绕X轴旋转,也叫翻滚角. UE4里,蓝图中的rotation的三个依次为roll,pitch,yaw.C++中 ...

  3. KD-tree 专题「Hide and Seek · 巧克力王国」

    Lockey的瞎理解 抄了一遍板子又水了俩题,感觉对KD-tree 稍稍理解了一点儿,唠叨一下(二维的KD-tree),如有错误请指出(Lockey 洗脸恭听) 普通平衡树维护的是一维的序列,但对于二 ...

  4. Kubernetes容器集群管理环境 - Prometheus监控篇

    一.Prometheus介绍之前已经详细介绍了Kubernetes集群部署篇,今天这里重点说下Kubernetes监控方案-Prometheus+Grafana.Prometheus(普罗米修斯)是一 ...

  5. 2019上半年总结——Github上那些Java面试、学习相关仓库

    分享一下最近逛Github看到了一些对于Java面试以及学习有帮助的仓库,这些仓库涉及Java核心知识点整理.Java常见面试题.算法.基础知识点比如网络和操作系统等等. 知识点相关 1.JavaGu ...

  6. 【JDK】JDK源码分析-AbstractQueuedSynchronizer(2)

    概述 前文「JDK源码分析-AbstractQueuedSynchronizer(1)」初步分析了 AQS,其中提到了 Node 节点的「独占模式」和「共享模式」,其实 AQS 也主要是围绕对这两种模 ...

  7. springboot管理类,springboot注入类

    springboot管理类,springboot注入类 定义一个配置类,添加@Configuration注解,EvaluatorTemplate代表你需要注入的第三方类 @Configuration ...

  8. 2019年一半已过,这些大前端技术你都GET了吗?- 下篇

    在上一篇文章中已经介绍了大前端关于状态管理.UI组件.小程序.跨平台和框架层的内容.在本文中,我会继续介绍编程语言.工程化.监控.测试和服务端,同时也会对下半年大前端可以关注的部分进行展望. 结合个人 ...

  9. python3 编译安装

    前言: Linux下大部分系统默认自带python2.x的版本,最常见的是python2.6或python2.7版本,默认的python被系统很多程序所依赖,比如centos下的yum就是python ...

  10. 微信公众号接入服务器验证(Go实现)

    1 基本流程 将token.timestamp.nonce三个参数进行字典序排序 将三个参数字符串拼接成一个字符串进行sha1加密 开发者获得加密后的字符串可与signature对比,标识该请求来源于 ...