SPOJ:Harbinger vs Sciencepal(分配问题&不错的DP&bitset优化)
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优化)的更多相关文章
- hdu 5745 La Vie en rose DP + bitset优化
http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...
- HDU5745-La Vie en rose-字符串dp+bitset优化
这题现场的数据出水了,暴力就能搞过. 标解是拿bitset做,转移的时候用bitset优化过的操作(与或非移位)来搞,复杂度O(N*M/w) w是字长 第一份标程的思路很清晰,然而后来会T. /*-- ...
- hdu5745 La Vie en rose 巧妙地dp+bitset优化+滚动数组减少内存
/** 题目:hdu5745 La Vie en rose 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5745 题意:题目给出的变换规则其实就是交换相邻 ...
- SPOJ:Collecting Candies(不错的DP)
Jonathan Irvin Gunawan is a very handsome living person. You have to admit it to live in this world. ...
- HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
Bipartite Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5890 Eighty seven(DP+bitset优化)
题目链接 Eighty seven 背包(用bitset预处理)然后对于每个询问O(1)回答即可. 预处理的时候背包. #include <bits/stdc++.h> using nam ...
- bzoj3687简单题(dp+bitset优化)
3687: 简单题 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 861 Solved: 399[Submit][Status][Discuss] ...
- Harbinger vs Sciencepal
Harbinger vs Sciencepal 题意:给你n对人, 每一对都有2个人,每个人分别有一个值, 现在将每队人拆开塞入2组,要求分完这n对人之后,2个组的差值最小. 题解:将每队人的差值算出 ...
- HDU3480_区间DP平行四边形优化
HDU3480_区间DP平行四边形优化 做到现在能一眼看出来是区间DP的问题了 也能够知道dp[i][j]表示前 i 个节点被分为 j 个区间所取得的最优值的情况 cost[i][j]表示从i ...
随机推荐
- Redis数据结构之简单动态字符串
Redis没有直接使用C语言传统的字符串表示(以空字符结尾的字符数组), 而是自己构建了一种名为简单动态字符串(simple dynamic string,SDS)的抽象类型, 并将SDS用作Redi ...
- python判断一个字符串是否是小数
最近在写代码的时候,发现一个问题,想判断一个字符串是不是一个合法的小数,发现字符串没有内置判断小数的方法,然后就写了一个判断字符串是否是小数,可以判断正负小数,代码如下: 1 2 3 4 5 6 ...
- Gradle讲解
简介: Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置. ...
- BZOJ——1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
http://www.lydsy.com/JudgeOnline/problem.php?id=1607 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 2 ...
- Wannafly练习赛14
B(倍增) 题意: 分析: 先可以用two point预处理出以每个位置为起点的连续段<=k的下一个终点 然后对于每个询问,倍增跳就行了 时间复杂度O(nlogn) C(扫描线处理区间询问) ...
- python的分布式队列神器 Celery
pip search kafka可以搜索到很多kafka的python插件,选择一个大家用的比较多的 celery结合kafka使用 使用kafka扩展python logging集中式日志收集 re ...
- 转:一个android开发者独立开发社交app全过程
http://www.cnblogs.com/linguanh/p/5683069.html
- MySQL Community Server 5.6和MySQL Installer 5.6
mysql community server是mysql社区版的数据库服务器.即数据库软件. mysql installer是mysql软件的安装管理器,能够通过installer来选择安装mysql ...
- Json——使用Json jar包实现Json字符串与Java对象或集合之间的互相转换
总结一下利用Json相关jar包实现Java对象和集合与Json字符串之间的互相转换: 1.创建的User类: package com.ghj.packageofdomain; public clas ...
- 数据结构与算法之贪心算法 C++实现
1.基本思路:从问题的某一个初始解触发逐步逼近给定的目标,以尽可能快的求得更好的解. 当达到算法中某一步不能再继续前进时.就停止算法,给出近似值.也就是说贪心算法并不从总体最优考虑,它所作出的选择仅仅 ...