One of DB and TN common interests is traveling. One day, they went to Grand Line and found One Piece !

The One Piece treasure has n gold coins (n is even). Both them like gold coins, but they evaluate them as different values. So they decided to divide those coins by following method :

DB and TN do n / 2 steps, at each step, DB choose 2 coins, TN takes the coin that she evaluates it greater, and DB take the rest coin.

Let’s help DB find how to take the maximum value at possible.

Input

First line : a single integer n (n is even) – the number of coins

Second line : n integers a1, a2, …, an. ai is the value of ith coin that TN evaluates.

Third line : n integers b1, b2, …, bn. bi is the value of ith coin that DB evaluates.

Output

First line : an integer S – the maximum value DB can take.

Last n / 2 lines : ith line contains two number x and y (1 ≤ x, y ≤ n), are the indexes of two 
coins that DB choose on ith step. Each coin must be chose exact one time.

If there are multiple ways, just print any of them.

Constraints

2 ≤ n ≤ 500 000

1 ≤ ai ≤ 109

1 ≤ bi ≤ 109

Note that a1, a2, …, an are n distinct integers.

Example

Input:
6
6 10 11 18 5 14
1 7 6 12 15 1 Output: 
28
5 1
2 6
3 4

Warning: large Input/Output data, be careful with certain languages

题意:有N个物品(N是偶数),对于每个物体,A同学和B同学都有自己的估值。每次A同学取出两个物体,B取走自己认为价值高的一个,然后A拿走剩下的一个,问A应该如果选择。使得A最后获得的价值最高。

思路:我们按B认为的价值排序,假设排成一排,左边大于右边,那么问题就成了:

 The problem:有N个人,从左到右排除一排,每个人有自己的权值,现在需要两两匹配成N/2队,每队的贡献是这一队的右边这个人的权值(即在原题中左边的被B取走了),问如何匹配贡献最大。

错误思路:用优先队列,一开始把除了最右边的那个都放进队列里,从最右边开始匹配,找他左边的还没有被匹配的最小值匹配; 比如12 1 6 7 2 15。得到错误答案24。

正确打开方式:我们先假设把偶数的取走,然后如果右边的未取走的比它大,则可以替换。那么可以用单调队列搞。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct in{
int a,b,id;
in(){ a=b=id=;}
in(int bb,int ii):b(bb),id(ii){}
bool friend operator <(in x,in y){
return x.b>y.b;
}
}s[maxn];
bool cmp(in w,in v){ return w.a>v.a; }
int vis[maxn];
set<in>Set;
int L[maxn],R[maxn];
int main()
{
int N,i,j; long long ans=;
scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d",&s[i].a);
for(i=;i<=N;i++) scanf("%d",&s[i].b);
for(i=;i<=N;i++) s[i].id=i;
sort(s+,s+N+,cmp);
for(i=N;i>=;i--){
if(i%==){
if(Set.empty()) ans+=s[i].b,vis[s[i].id]=;
else {
set<in>::iterator it=Set.begin();
if((*it).b>s[i].b){
ans+=(*it).b; vis[(*it).id]=;
Set.insert(in(s[i].b,s[i].id));
Set.erase(it);
}
else {
vis[s[i].id]=; ans+=s[i].b;
}
}
}
else Set.insert(in(s[i].b,s[i].id));
}
int sum1=,sum2=;
for(i=N;i>=;i--){
if(vis[s[i].id]) L[++sum1]=s[i].id;
else R[++sum2]=s[i].id;
}
printf("%lld\n",ans);
for(i=;i<=N/;i++) printf("%d %d\n",L[i],R[i]);
return ;
}

SPOJ:One piece(不错的带权括号最大匹配问题)的更多相关文章

  1. HDU 2255 奔小康赚大钱(带权二分图最大匹配)

    HDU 2255 奔小康赚大钱(带权二分图最大匹配) Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊 ...

  2. Luogu 1559 运动员最佳匹配问题(带权二分图最大匹配)

    Luogu 1559 运动员最佳匹配问题(带权二分图最大匹配) Description 羽毛球队有男女运动员各n人.给定2 个n×n矩阵P和Q.P[i][j]是男运动员i和女运动员j配对组成混合双打的 ...

  3. UVA1349(带权二分图最大匹配 --> KM算法模板)

    UVA1349 题意:给定一些有向带权边,求出把这些边构造成一个个环,总权值最小 解法: 对于带权的二分图的匹配问题可以用通过KM算法求解. 要求最大权匹配就是初始化g[i][j]为0,直接跑就可以: ...

  4. 带权二分图最大匹配KM算法

    二分图的判定 如果一个图是连通的,可以用如下的染色法判定是否二分图: 我们把X部的结点颜色设为0,Y部的颜色设为1. 从某个未染色的结点u开始,做BFS或者DFS .把u染为0,枚举u的儿子v.如果v ...

  5. "《算法导论》之‘图’":不带权二分图最大匹配(匈牙利算法)

    博文“二分图的最大匹配.完美匹配和匈牙利算法”对二分图相关的几个概念讲的特别形象,特别容易理解.本文介绍部分主要摘自此博文. 还有其他可参考博文: 趣写算法系列之--匈牙利算法 用于二分图匹配的匈牙利 ...

  6. POJ 2195 Going Home | 带权二分图匹配

    给个地图有人和房子 保证人==房子,每个人移动到房子处需要花费曼哈顿距离的代价 问让人都住在房子里最小代价 显然是个带权二分图最大匹配 转化成以一个网络,规定w是容量,c是代价 1.S向人连边,w=1 ...

  7. hdu4829 带权并查集(题目不错)

    题意: Information Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  8. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  9. POJ1417:True Liars(DP+带权并查集)

    True Liars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. hdu 4885 (n^2*log(n)判断三点共线建图)+最短路

    题意:车从起点出发,每次只能行驶L长度,必需加油到满,每次只能去加油站或目的地方向,路过加油站就必需进去加油,问最小要路过几次加油站. 开始时候直接建图,在范围内就有边1.跑最短了,再读题后发现,若几 ...

  2. ZOJ 3717 二分+2-sat判定。

    好久没有2-sat了,此题当复习之用,二分求最大值+2-sat判断可行,此题主要跪于题意:The results should be rounded to three decimal places. ...

  3. GRYZY #13. 拼不出的数

    拼不出的数 lost.in/.out/.cpp [问题描述] 3 个元素的集合 {5, 1, 2} 的所有子集的和分别是 0, 1, 2, 3, 5, 6, 7, 8.发 现最小的不能由该集合子集拼出 ...

  4. 框架-数据库定义MD5加密

    1.--定义Md5加密declare @pt_pwd varchar(50)set @pt_pwd = ''set @pt_pwd = substring(sys.fn_sqlvarbasetostr ...

  5. SolidEdge 工程图中如何显示彩色工程图

    点击这个按钮,然后更新视图   效果如下图所示,注意如果你的装配图(.dft文件)是单独拷贝出来的,装配图所引用的零件无法追溯到,则无法渲染这些零件,因此无法制作彩色工程图.

  6. HDU 2825 Wireless Password (AC自己主动机,DP)

    pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...

  7. Android eclipse 项目R文件无法生成

    这个问题相信非常多人从网上已经搜到不少的解决方式. android 无法生成R文件 是件痛苦的事情.即使有时候你xml文件没有错误,他都无法生成. 针对此问题,我总结下面几个方面的原因. 一.xml本 ...

  8. C#.net word excel powerpoint (ppt) 转换成 pdf 文件

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  9. v-model在其它元素以及类型上的用法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. C++结构体中使用函数与类中使用函数小结

    #include <iostream>#include <string.h>using namespace std;struct stud//学生信息结构体{    char ...