codeforces 1185G1 状压dp

codeforces 1185G1. Playlist for Polycarp (easy version)(动态规划)

传送门:https://codeforces.com/contest/1185/problem/G1

题意:

你从学校回到家要T的时间,你现在有n首歌,每首歌的播放时间为ti,编号为gi,你现在想要确定播放一些歌使得你正好用T分钟听完这些歌,且每次连续播放的两首歌编号不同。问你有多少种播放方法,注意顺序不同视为两种方法。

题解:

代码:

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef long long ll;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********
")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]
"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
"
#define forn(i, n) for (int i = 0; i < int(n); i++)

const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
struct EDGE {
    int v, nxt;
} edge[maxn << 1];
int head[maxn], tot;
void add_edge(int u, int v) {
    edge[tot].v = v, edge[tot].nxt = head[u], head[u] = tot++;
}
int dp[1 << 16][4];
int t[maxn], g[maxn];
int main() {
#ifndef ONLINE_JUDGE
    FIN
#endif
    int n, T;
    scanf("%d%d", &n, &T);
    for(int i = 0; i < n; i++) {
        cin >> t[i] >> g[i];
        g[i]--;
    }
    int result = 0;
    dp[0][3] = 1;
    for(int sta = 0; sta < 1 << n; sta++) {
        for(int i = 0; i < 4; i++) {
            for(int j = 0; j < n; j++) {
                if (g[j] != i && ((sta & (1 << j)) == 0))
                    dp[sta ^ (1 << j)][g[j]] = (dp[sta ^ (1 << j)][g[j]] + dp[sta][i]) % mod;
            }
            int sum = 0;                                                             
            for(int j = 0; j < n; j++) {
                // debug1(sum);
                if (sta & (1 << j)) {
                    sum += t[j];
                }
            }
            if (sum == T)
                result = (result + dp[sta][i]) % mod;
        }
    }

    cout << result << endl;
    return 0;
}
每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
原文地址:https://www.cnblogs.com/buerdepepeqi/p/11163681.html