zoukankan      html  css  js  c++  java
  • 2020杭电多校联合训练(第四场) C.Contest of Rope Pulling (01背包指令集优化)

    题面

    Problem Description
    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 test cases follow.

    For each test case, the first line contains two integers n,m(1≤n,m≤1000), 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≤wi≤1000,−109≤vi≤109), 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 104.

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

    Sample Input
    2
    3 4
    4 7
    3 8
    2 2
    1 4
    5 8
    1 3
    4 4
    1 2
    1000 -10000
    200 3000
    800 5000

    Sample Output
    30
    0

    思路

    暴力跑背包一定是会超时的,所以我们考虑指令集优化,复杂度n3.正解的话,呶,看下面,我看不懂...

    代码实现

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
    #define per(i,n,a) for (int i=n;i>=a;i--)
    #define MT(x,i) memset(x,i,sizeof(x) )
    #define rev(i,start,end) for (int i=0;i<end;i++)
    #define inf 0x3f3f3f3f3f3f3f3f
    #define mp(x,y) make_pair(x,y)
    #define lowbit(x) (x&-x)
    #define MOD 1000000007
    #define exp 1e-8
    #define N 1000005 
    #define fi first 
    #define se second
    #define pb push_back
    typedef long long ll;
    typedef pair<int ,int> PII;
    ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
    inline int read() {
        char ch=getchar(); int x=0, f=1;
        while(ch<'0'||ch>'9') {
            if(ch=='-') f = -1;
            ch=getchar();
        } 
        while('0'<=ch&&ch<='9') {
            x=x*10+ch-'0';
            ch=getchar();
        }   return x*f;
    }
    const int maxn=1e6+10;
    ll dp[maxn];
    ll dp2[maxn];
    int t;
    int n,m;
    
    void solve () {
        int sum1=0,sum2=0;
        rep (i,1,n) {
            int x,y;
            cin>>x>>y;
            per (j,sum1,0) {
                if (dp[j]+y>dp[j+x]) dp[j+x]=dp[j]+y;
            }
            sum1+=x;
        }
        rep (i,1,m) {
            int x,y;
            cin>>x>>y;
            per (j,sum2,0) {
                if (dp2[j]+y>dp2[j+x]) dp2[j+x]=dp2[j]+y;
            }
            sum2+=x;
        }
        ll ans=0;
        int sum=min (sum1,sum2);
        rep (i,1,sum) {
            ans=max (ans,dp[i]+dp2[i]);
        }
        cout<<ans<<endl;
        rep (i,1,sum2) dp[i]=-1e15;
        rep (i,1,sum2) dp2[i]=-1e15; 
    }
    
    int main () {
       cin>>t;
       rep (i,1,maxn) {
           dp[i]=dp2[i]=-1e15;
       }
       while (t--) {
           cin>>n>>m;
           solve ();
       }
        return 0;
    }
    
    
  • 相关阅读:
    第一次作业-编译原理概述
    文法和语言总结与梳理(作业四)
    作业三
    作业二
    编译原理概述
    编译原理 作业九
    编译原理 作业八
    编译原理 作业七
    编译原理 作业六
    编译原理 作业五
  • 原文地址:https://www.cnblogs.com/hhlya/p/13412234.html
Copyright © 2011-2022 走看看