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. 结构字段验证--validator.v9

    官网:https://godoc.org/gopkg.in/go-playground/validator.v9#hdr-Baked_In_Validators_and_Tags package va ...

  2. AC日记——栈 洛谷 P1044

    题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈). 栈的重要性不言自明,任何 ...

  3. AC日记——L国的战斗之间谍 洛谷 P1916

    题目背景 L国即将与I国发动战争!! 题目描述 俗话说的好:“知己知彼,百战不殆”.L国的指挥官想派出间谍前往I国,于是,选人工作就落到了你身上. 你现在有N个人选,每个人都有这样一些数据:A(能得到 ...

  4. 什么是 Linux

    什么是Linux Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络 ...

  5. Crypto另外两段加密解密的代码

    第一段代码风格-平铺直叙: import sys from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex class p ...

  6. HDU1800 Flying to the Mars 【贪心】

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. 一起学习CMake – 01

    一起学习CMake – 01 本节介绍CMake里最常用的三个命令,分别是cmake_minimum_required; project; add_executable等. CMake是个好东西,在使 ...

  8. linux 进程间通信之 消息队列

    消息队列就是一个消息的链表. 能够把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程能够向中依照一定的规则加入新消息.有读权限的进程则能够读走消息. 读走就没有了.消息队列是 ...

  9. Android使用procrank和dumpsys meminfo 、top分析内存占用情况

    如果你想查看所有进程的内存使用情况,可以使用命令procrank.dumpsys meminfo查看,当然也只可以过滤出某个进程如:dumpsys meminfo | grep -i phone 先来 ...

  10. 关于Java中强制类型转换的问题

    为了更好的理解我们先看下面的例子: package com.yonyou.test; import java.util.ArrayList; import java.util.Iterator; im ...