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 ...
随机推荐
- 用多线程优化Excel表格数据导入校验的接口
公司的需求,当前某个Excel导入功能,流程是:读取Excel数据,传入后台校验每一条数据,判断是否符合导入要求,返回给前端,导入预览展示.(前端等待响应,难点).用户再点击导入按钮,进行异步导入(前 ...
- 3. 源码分析---SOFARPC客户端服务调用
我们首先看看BoltClientProxyInvoker的关系图 所以当我们用BoltClientProxyInvoker#invoke的时候实际上是调用了父类的invoke方法 ClientProx ...
- 2月9日 《Java 8实战》读后感
第一部分 基础知识 第3章 Lambda表达式 使用函数式接口 Predicate Consumer Function 第二部分 函数式数据处理 第4章 引入流 第5章 使用流 第6章 用流收集数据 ...
- OSGi Bundle之Hello World
开发一个简单的Hello World的OSGi Bundle(OSGi绑定包) 在OSGi中,软件是以Bundle的形式发布的.一个Bundle由Java类和其它资源构成,它可为其它的Bundle提供 ...
- .netcore持续集成测试篇之测试方法改造
系列目录 通过前面两节讲解,我们的测试类中已经有两个测试方法了,总体上如下 public class mvc20 { private readonly HttpClient _client; publ ...
- java优雅注释原则和代码格式列举
一.java的三种注释类型 单行注释:// ...... 块注释:/* ...... */ 文档注释:/** ...... */ 二.指导原则 注释不能美化糟糕的代码,碰到糟糕的代码就重新写吧. 用代 ...
- Go类型别名与类型定义区别
类型别名和自定义类型区别 自定义类型 //自定义类型是定义了一个全新的类型 //将MyInt定义为int类型 type MyInt int 类型别名 //类型别名规定:TypeAlias只是Type的 ...
- centos7.x 安装系统/配置网络/设置主机名
1.安装系统 系统的安装就不多说了,自行查找百度,如:https://www.cnblogs.com/wcwen1990/p/7630545.html 2.配置网络(局域网上网) 修改配置 ...
- Redis集群环境下的键值空间监听事件实现方案
一直想记录工作中遇到的问题和解决的方法,奈何没有找到一方乐土,最近经常反思,是否需要记录平时的点滴,后台还是决定下定决心记录一些,以便以后用到的时候找不着,实现这样的一个功能主要也是业务所需要的. 需 ...
- Linux 终端命令格式
Linux 终端命令格式 一.目标 了解终端命令格式 知道如何查阅终端命令帮助信息 二. 终端命令格式 command [-options] [parameter] 说明: command:命令名,相 ...