Rainbow 6 is a very popular game in colleges. There are 2 teams, each having some members and the 2 teams play some matches against each other. The team which wins the maximum number of matches wins the game! Two of my friends Ashank and Aditya (better known as Harbinger and Sciencepal) are great gamers and they want to compete. So they decide to form their own teams.

There are 2*N players who are interested to be a part of their team. Each player has some rating (based on his performance) and Akarsh(Nimbus) is responsible for forming their teams. Being a good friend of both, he wants to form two teams such that the difference of total ratings of the players between the teams becomes minimum. The players come to him in pairs and he has to put one of them in Harbinger's team and the other in Sciencepal's team at that instant. This is a tedious task for him and therefore he needs your help!

Input

The first line of the input contains an integer T denoting the number of the test cases. (1 ≤ T ≤ 10)

First line of each test case contains a number N denoting the number of pair of players. (1 ≤ N ≤ 200)

Next N lines contains rating of the persons in pairs as x and y. (0 ≤ x , y ≤ 250)

Output

For each test case, print a single integer denoting the minimum possible absolute rating difference between Sciencepal's and Harbinger's team.

Example

Input:

1
2
2 3
4 5

Output:
0

题意:给定N对人,每对人,其中一个分给A班,另一个给B班。每个人都有自己的价值Xi,现在问如何分班使得两个班的价值差最小。(N<200;Xi<=2500;每个点有<=T=10组数据,时x限1s)

思路:每对的差值绝对值a,累加和为sum;即求最靠近sum/2的背包。用bitset优化一下。

#include<cstdio>
#include<cstdlib>
#include<bitset>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
bitset<maxn>S;
int a[];
int main()
{
int T,N,ans,i,x,y;
scanf("%d",&T);
while(T--){
S.reset(); ans=;
scanf("%d",&N);
for(i=;i<=N;i++){
scanf("%d%d",&x,&y);
a[i]=abs(x-y);
ans+=a[i];
}
S[]=;
for(i=;i<=N;i++){
S|=S<<a[i];
}
for(i=ans/;i>=;i--){
if(S[i]==){ cout<<ans-i-i<<endl;
break;
}
}
}
return ;
}

SPOJ:Harbinger vs Sciencepal(分配问题&不错的DP&bitset优化)的更多相关文章

  1. hdu 5745 La Vie en rose DP + bitset优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...

  2. HDU5745-La Vie en rose-字符串dp+bitset优化

    这题现场的数据出水了,暴力就能搞过. 标解是拿bitset做,转移的时候用bitset优化过的操作(与或非移位)来搞,复杂度O(N*M/w) w是字长 第一份标程的思路很清晰,然而后来会T. /*-- ...

  3. hdu5745 La Vie en rose 巧妙地dp+bitset优化+滚动数组减少内存

    /** 题目:hdu5745 La Vie en rose 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5745 题意:题目给出的变换规则其实就是交换相邻 ...

  4. SPOJ:Collecting Candies(不错的DP)

    Jonathan Irvin Gunawan is a very handsome living person. You have to admit it to live in this world. ...

  5. HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】

    Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. HDU 5890 Eighty seven(DP+bitset优化)

    题目链接 Eighty seven 背包(用bitset预处理)然后对于每个询问O(1)回答即可. 预处理的时候背包. #include <bits/stdc++.h> using nam ...

  7. bzoj3687简单题(dp+bitset优化)

    3687: 简单题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 861  Solved: 399[Submit][Status][Discuss] ...

  8. Harbinger vs Sciencepal

    Harbinger vs Sciencepal 题意:给你n对人, 每一对都有2个人,每个人分别有一个值, 现在将每队人拆开塞入2组,要求分完这n对人之后,2个组的差值最小. 题解:将每队人的差值算出 ...

  9. HDU3480_区间DP平行四边形优化

    HDU3480_区间DP平行四边形优化 做到现在能一眼看出来是区间DP的问题了 也能够知道dp[i][j]表示前  i  个节点被分为  j  个区间所取得的最优值的情况 cost[i][j]表示从i ...

随机推荐

  1. VMware View 要求操作句柄的状态错误

    win10系统安装的VMware-viewclient,版本是

  2. luogu P1342 请柬

    题目描述 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣传剧院,尤其是古色古香的喜剧片.他们已经打印请帖和所有必要的信息和计划.许多学生被雇来分发这些请 ...

  3. 算法笔记字符串处理问题H:编排字符串(2064)

    题目描述 请输入字符串,最多输入4 个字符串,要求后输入的字符串排在前面,例如 输入:EricZ 输出:1=EricZ 输入:David 输出:1=David 2=EricZ 输入:Peter 输出: ...

  4. File类 文件过滤器

    创建过滤器 package cn.zmh.File; import java.io.File; import java.io.FileFilter; // 实现FileFilter类的功能 publi ...

  5. 团购类网站倒计时的js实现

    一.如火如荼的团购网站 根据易观国际提供的统计数据,截至2010年6月,中国市场团购网站数量已经突破400家.国内团购潮从今年2月份开始出现,在4~6月出现高峰,尤其是今年5月,一些大的网站如爱帮网. ...

  6. Spring boot 整合spring Data JPA+Spring Security+Thymeleaf框架(上)

    近期上班太忙所以耽搁了给大家分享实战springboot 框架的使用. 以下是spring boot 整合多个框架的使用. 首先是准备工作要做好. 第一  导入框架所需的包,我们用的事maven 进行 ...

  7. Redhat常用指令

    yum 部分常用的命令包括: 自动搜索最快镜像插件:yum install yum-fastestmirror 安装yum图形窗口插件:yum install yumex 查看可能批量安装的列表:yu ...

  8. 使用 Unicode 编码

    面向公共语言执行库的应用程序使用编码将字符表示形式从本机字符方案(Unicode)映射为其它方案. 应用程序使用解码将字符从非本机方案(非 Unicode)映射为本机方案. System.Text 命 ...

  9. bootstrap之ScrollTo

    ScrollTo package io.appium.android.bootstrap.handler; import com.android.uiautomator.core.UiObject; ...

  10. RESTful Web Service