HDU- 6883 Coin Game (背包)

HDU- 6883 Coin Game (背包)

题意:

给你\(\mathit n\)个按钮,每一个按钮可以按三次,每一次可以得到的价值分别为\(a_i,b_i,a_i\)

\(f(i)\) 代表你可以按\(\mathit i\)次按钮时得到的最大价值,

现在给定一个\(\mathit m\),让你求$ f(1)⊕f(2)⊕f(3)⊕⋯⊕f(m)$。

思路:

对于每一个按钮,因为想拿后面的收益就必须也要拿前面的,所以我们可以理解为:

花费成本分别为\(1,2,3\), 可以得到\(a_i,a_i+b_i,2*a_i+b_i\)

又可以拆分为两个物品,价值和成本分别是:

1、成本为\(\text 1\) , 价值为\(a_i\)

2、成本为 \(\text 2\), 价值为 \(a_i+b_i\)

对这两个物品的所有选择组合(都不取,只取其中一个,都取)和对该按钮的4种(0,1,2,3次)按的次数构成的成本和收益恰好相等。

那么问题就转化为\(2*n\)个物品的背包问题,

简单的贪心一下即可,

每一个状态\(f(i)\) 最优的转移一定是从\(f(i-1),f(i-2)\)中的一个转移过来。

我们将物品按照成本分为两类后降序排序,然后维护两个下标进行转移即可。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 5000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
const int threshold = 10000000;
unsigned long long k1, k2;

unsigned long long xorShift128Plus() {
    unsigned long long k3 = k1, k4 = k2;
    k1 = k4;
    k3 ^= (k3 << 23);
    k2 = k3 ^ k4 ^ (k3 >> 17) ^ (k4 >> 26);
    return k2 + k4;
}

int a[maxn];
int b[maxn];
void get(int n)
{
    repd(i, 1, n)
    {
        a[i] = xorShift128Plus() % threshold + 1;
        b[i] = xorShift128Plus() % threshold + 1;
    }
}
int n, m;
int main()
{
#if DEBUG_Switch
    freopen("C:\\code\\input.txt", "r", stdin);
#endif
    //freopen("C:\\code\\output.txt","w",stdout);
    while (~scanf("%d %d %llu %llu", &n, &m, &k1, &k2))
    {
        get(n);
        repd(i, 1, n)
        {
            b[i] += a[i];
        }
        sort(a + 1, a + 1 + n, greater<int>() );
        sort(b + 1, b + 1 + n, greater<int>() );
        int pos[2] = {1, 1};
        ll ans = 0ll;
        ll now = 0ll;
        // 我的这种处理方式会越到n+1位置访问。
        a[n + 1] = 0;
        b[n + 1] = 0;
        repd(i, 1, m)
        {
            if (pos[0] == 1)// 这时只能取成本为1的。
            {
                now += a[pos[0]++];
            } else
            {
                if (b[pos[1]] - a[pos[0] - 1] > a[pos[0]])// 判断是否取成本为2的更优。
                {
                    now += b[pos[1]++] - a[--pos[0]];
                } else
                {
                    now += a[pos[0]++];
                }
            }
            ans ^= now;
        }
        printf("%lld\n", ans );
    }
    return 0;
}



posted @ 2020-09-06 11:38  茄子Min  阅读(256)  评论(0编辑  收藏  举报