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 ...
随机推荐
- Django安装 测试、导入项目以及运行开发服务器
安装Django 下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py install 增加环境变量: C:\Python27\Scripts 测试djang ...
- poj 3714 寻找最近点对
参考自<编程之美>169页,大概原理就是把区间分成两部分,然后递归找每一部分中最近的点对,还有一种情况就是这个点对分属于这两部分,然后选两部分中的部分点枚举即可,取其最小值. //2013 ...
- HashMap常见面试题整理
花了三天时间来仔细阅读hashMap的源码,期间补了下不少数据结构的知识,刷了不少相关的面试题并进行了整理 1.谈一下HashMap的特性? 1.HashMap存储键值对实现快速存取,允许为null. ...
- 基于JaCoCo的Android测试覆盖率统计(二)
> 本文章是我上一篇文章的升级版本,详见地址:https://www.cnblogs.com/xiaoluosun/p/7234606.html ## 为什么要做这个?1. 辛辛苦苦写了几百条测 ...
- php sql 类似 mybatis 传参
PHP sql 处理上,没有类似于 java mybatis 的工具,导致进行一些sql 处理时,会有诸多不便, 楼主抽时间写了一个 php 类似 mybatis 的sql 工具,省去了拼装sql 的 ...
- Python学习系列(四)Python 入门语法规则2
Python学习系列(四)Python 入门语法规则2 2017-4-3 09:18:04 编码和解码 Unicode.gbk,utf8之间的关系 2.对于py2.7, 如果utf8>gbk, ...
- Cannot attach the file “MvcMovie.mdf” as database “aspnet-MvcMovie”
今天在微软开发人员官网上学习asp.net mvc5入门的时候,遇到一个棘手的问题,我是按照教程一步一步操作的,但期间遇到一个自己觉得莫名其妙的问题,教程中也没有提到这个, 在添加新字段这一章节,跟着 ...
- 前端插件之Datatables使用--上篇
工欲善其事,必先利其器 本系列文章介绍我在运维系统开发过程中用到的那些顺手的前端插件,前边两篇分别介绍了Duallistbox插件和Select2插件的使用,这一篇开始Databases的征服之旅 D ...
- 0x33 同余
目录 定义 同余类与剩余系 费马小定理 欧拉定理 证明: 欧拉定理的推论 证明: 应用: 定义 若整数 $a$ 和整数 $b$ 除以正整数 $m$ 的余数相等,则称 $a,b$ 模 $m$ 同余,记为 ...
- TI MSP430工程配置及2019年电赛A题编程示例(使用430 F5529)
配置 第一步:右击工程,选择Options 第二步:在General Options的Target选项卡里选择对应的器件Device,这里是MSP430G2231 第三步:在Debugger里选择FE ...