ZOJ 3812 We Need Medicine 01背包+位最佳化

來源:互聯網
上載者:User

標籤:http   io   os   ar   for   sp   on   c   amp   

題目連結:點擊開啟連結

#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>#include <map>typedef unsigned long long ll;using namespace std;const int N = 400 + 1;const int M = 200000 + 1;int t[N], w[N], g[51][M], ans[N], dep;ll d[M];map<ll, int> id;char ch;void get(int& z) {while ((ch = getchar()) > '9' || ch < '0');z = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9')z = z * 10 + ch - '0';}int main() {for (ll i = 1; i <= 50; ++i)id[1ll << i] = i;int cas, n, m, x, y;ll v, mx = (1ll << 51) - 1;get(cas);while (cas -- > 0) {memset(d, 0, sizeof d);d[0] = 1;memset(g, -1, sizeof g);get(n); get(m);for (int i = 0; i < n; ++i) {get(w[i]); get(t[i]);for (int j = M - t[i] - 1; j >= 0; --j)if (d[j] > 0) {v = d[j + t[i]];d[j + t[i]] |= (d[j] << w[i]) & mx;v = v ^ d[j + t[i]]; while (v > 0) {if (v & 1) {v = v & (v - 1);continue;}x = id[v & (v - 1) ^ v];g[x][j + t[i]] = i;v = v & (v - 1);}}//g[w[i]][t[i]] = i;}while (m -- > 0) {get(x); get(y);if (g[x][y] >= 0) {dep = 0;while (x > 0 && y > 0) {ans[dep++] = g[x][y];x -= w[ans[dep - 1]];y -= t[ans[dep - 1]];}for (int i = dep - 1; i >= 0; --i) {if (i == 0)printf("%d\n", ans[i] + 1);elseprintf("%d ", ans[i] + 1);}} else puts("No solution!");}}return 0;}


ZOJ 3812 We Need Medicine 01背包+位最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.