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. Mac相关快捷键操作

    拷贝: shift + option + 拖动拖动至目的地 创建快捷方式: option + command + 拖动至目的地

  2. 破解EFCore扩展Dll --- Z.EntityFramework.Extensions.EFCore

    安装 Z.EntityFramework.Extensions.EFCore Install-Package Z.EntityFramework.Extensions.EFCore -Version ...

  3. bat 搜索进程名并kill

    @echo off set/p "target=进程名(默认nginx): "if not defined target (set "target=nginx" ...

  4. Mybatis获取代理对象

    mybatis-config.xml里标签可以放置多个environment,这里可以切换test和develop数据源 databaseIdProvider提供多种数据库,在xml映射文件里选择da ...

  5. NS3中一些难以理解的常数

    摘要:在NS3的学习中,PHY MAC中总有一些常数,需要理解才能修改.如帧间间隔等.那么,本文做个简单分析,帮助大家理解.针对802.11标准中MAC协议.   void WifiMac::Conf ...

  6. CODING 告诉你如何建立一个 Scrum 团队

    原文地址:https://www.atlassian.com/agile/scrum/roles 翻译君:CODING 敏杰小王子 Scrum 当中有三个角色:PO(product owner),敏捷 ...

  7. 二.Google黑客语法

    搜索也是一门艺术! 说起Google,可谓是无人不知无人不晓,其强大的搜索功能,可以让你在瞬间找到你想要的一切.对于黑客而言,Google可是 一款绝佳的黑客工具.正因Google强大的检索能力,黑客 ...

  8. (18)ASP.NET Core 基于现有数据库创建EF模型(反向工程)

    1.简介 Entity Framework Core可通过数据库提供给应用程序的插件访问许多不同的数据库.我们可以通过使用Entity Framework Core构建执行基本数据访问的ASP.NET ...

  9. 解决pyinstaller打包可执行文件,存放路径包含中文无法运行的问题

    一.实验环境 1.Windows7x64_SP1 2.anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 二.问题描述 1.使用 ...

  10. Streaming-大数据的未来

    分享一篇关于实时流式计算的经典文章,这篇文章名为Streaming 101: The world beyond batch 那么流计算如何超越批处理呢? 从这几个方面说明:实时流计算系统,数据处理模式 ...