Contest of Rope Pulling-hdu 6804-Random Sorting

Rope Pulling, also known as Tug of War, is a classic game. Zhang3 organized a rope pulling contest between Class 1 and Class 2.

There are n students in Class 1 and m students in Class 2. The ith student has strength wi and beauty-value vi. Zhang3 needs to choose some students from both classes, and let those chosen from Class 1 compete against those chosen from Class 2. It is also allowed to choose no students from a class or to choose all of them.

To be a fair contest, the total strength of both teams must be equal. To make the contest more beautiful, Zhang3 wants to choose such a set of students, that the total beauty-value of all participants is maximized. Please help her determine the optimal total beauty-value.


Input
The first line of the input gives the number of test cases, T ( 1 ≤ T ≤ 30 ) T(1≤T≤30) T(1T30). T test cases follow.

For each test case, the first line contains two integers n , m ( 1 ≤ n , m ≤ 1000 ) n,m(1≤n,m≤1000) n,m(1n,m1000), representing the number of students in Class 1 and Class 2.

Then (n+m) lines follow, describing the students. The ith line contains two integers wi,vi ( 1 ≤ w i ≤ 1000 , − 1 0 9 ≤ v i ≤ 1 0 9 ) , (1≤wi≤1000,−10^9≤vi≤10^9), (1w i1000,109v i109), representing the strength and the beauty-value of the ith student. The first n students come from Class 1, while the other m students come from Class 2.

The sum of (n+m) in all test cases doesn’t exceed 1 0 4 10^4 104.


Output
For each test case, print a line with an integer, representing the optimal total beauty-value.

Idea
Use random number oscillation to mess around, the internal principle is not clear, but the extreme upper and lower bounds can be reduced through oscillation to optimize the array size and time complexity

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T>
inline void read(T &x)
{
    
    
    int tmp = 1;char c = getchar();x = 0;
    while (c > '9' || c < '0'){
    
    if (c == '-')tmp = -1;c = getchar();}
    while (c >= '0' && c <= '9'){
    
    x = x * 10 + c - '0';c = getchar();}
    x *= tmp;
}
const int inf=0x3f3f3f3f;
const int mod=998244353;
const int N=2e3+10;
const int M=1e6+10;
typedef pair<int,int> pii;
vector<pii>vec;
ll dp[M];
int main(){
    
    
	ios::sync_with_stdio(false);cin.tie(nullptr); cout.tie(nullptr);
	int T;read(T);
	srand(time(0));
	const int mid=1e5;
	while(T--){
    
    
		memset(dp,0x8f,sizeof dp);
		int n,m;read(n),read(m);
		vec.clear();
		for(int i=1;i<=n;++i){
    
    
			int w,v;read(w),read(v);
			vec.push_back({
    
    w,v});
		}
		for(int i=1;i<=m;++i){
    
    
			int w,v;read(w),read(v);
			vec.push_back({
    
    -w,v});
		}
		random_shuffle(vec.begin(),vec.end());
		dp[mid]=0;
		for(int i=0;i<n+m;++i){
    
    
			pii &p=vec[i];
			if(p.first>0){
    
    
				for(int i=mid<<1;i>=p.first;--i){
    
    
					if(dp[i]<dp[i-p.first]+p.second) dp[i]=dp[i-p.first]+p.second;
				}
			}
			else{
    
    
				for(int i=0;i<=mid*2+p.first;++i){
    
    
					if(dp[i]<dp[i-p.first]+p.second) dp[i]=dp[i-p.first]+p.second;
				}
			}
		}
		printf("%lld\n",dp[mid]);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/bloom_er/article/details/108069952