惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
人人都是产品经理
人人都是产品经理
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
GbyAI
GbyAI
博客园 - 司徒正美
美团技术团队
Vercel News
Vercel News
IT之家
IT之家
U
Unit 42
Y
Y Combinator Blog
罗磊的独立博客
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
V
Visual Studio Blog
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MyScale Blog
MyScale Blog
博客园 - 叶小钗
A
About on SuperTechFans
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
B
Blog RSS Feed

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
HDU 5890.Eighty seven |OhYee 博客
2017-08-05 · via OhYee 博客

题目

{% fold 点击显/隐题目 %}

Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach children how to add numbers up. Before the class, he will prepare N cards with numbers. The number on the i-th card is ai. In class, each turn he will remove no more than 3 cards and let students choose any ten cards, the sum of the numbers on which is 87. After each turn the removed cards will be put back to their position. Now, he wants to know if there is at least one solution of each turn. Can you help him?

The first line of input contains an integer t(t≤5), the number of test cases. t test cases follow. For each test case, the first line consists an integer N(N≤50). The second line contains N non-negative integers a1,a2,...,aN. The i-th number represents the number on the i-th card. The third line consists an integer Q(Q≤100000). Each line of the next Q lines contains three integers i,j,k, representing Mr.Fib will remove the i-th, j-th, and k-th cards in this turn. A question may degenerate while i=j, i=k or j=k.

For each turn of each case, output 'Yes' if there exists at least one solution, otherwise output 'No'.

1 12 1 2 3 4 5 6 7 8 9 42 21 22 10 1 2 3 3 4 5 2 3 2 10 10 10 10 11 11 10 1 1 1 2 10 1 11 12 1 10 10 11 11 12

No No No Yes No Yes No No Yes Yes

{% endfold %}

题解

按照动态规划的思想,有:
dp[i][j][k] = dp[i-1][j][k] | dp[i-1][j-1][k-num[i]]
其中 dp[i][j][k] 表示前 i 个数中选择 j 个数能否达到 k

虽然这道题涉及到的 i j k 都比较小,但是由于查询次数达到 100000,还是不能直接动态规划做

对于动态规划部分,可以使用bitset压缩,能够省去 k 的枚举
由于 dp[i][][] 的值取决于 dp[i-1][][] ,因此这一维是不需要单独记录的
dp[i] 表示选取 i 个数的情况下,bitset的各位能否达到
则可以得到转移方程 dp[t] |= dp[t-1] << num[i]
其中,<< num[i]表示把 dp[t-1] 整体左移 num[i] 位(相当于一次完成k维的工作)

需要特别注意的是,t 维的循环应该从大到小循环(因为省去了 i 的情况下,从小到大的运算顺序是错的)

另外,这道题的时限是比较紧的,各个部分都需要优化,输入也要用输入优化

代码

{% fold 点击显/隐代码 %}```cpp Eighty seven https://github.com/OhYee/sourcecode/tree/master/ACM 代码备份
//
#define debug
#include
//
/
#include
#include
#include
#include
#include
using namespace std;

const int maxn = 51;

int num[maxn];
bitset<90> dp[11];

int ans[maxn][maxn][maxn];
int n;

int read_int() {
char c;
int ans = 0;
while (c = getchar(), !(c >= '0' && c <= '9'))
;
while (c >= '0' && c <= '9') {
ans *= 10;
ans += (int)c - '0';
c = getchar();
}
return ans;
}

bool getAns(int a, int b, int c) {
if (ans[a][b][c] == -1) {
for (int i = 0; i <= 10; i++)
dp[i].reset();
dp[0][0] = 1;

    for (int i = 1; i <= n; ++i) {
        if (i != a && i != b && i != c)
            for (int t = 10; t >= 1; --t)
                dp[t] |= dp[t - 1] << num[i];
    }

    ans[a][b][c] = dp[10][87];
}
return ans[a][b][c] == 1;

}

int main() {
#ifdef debug
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int START = clock();
#endif

int T = read_int();
while (T--) {
    memset(ans, -1, sizeof(ans));

    n = read_int();
    for (int i = 1; i <= n; ++i)
        num[i] = read_int();

    int Q = read_int();
    int a[3];
    for (int q = 0; q < Q; ++q) {
        a[0] = read_int(), a[1] = read_int(), a[2] = read_int();
        sort(a, a + 3);
        printf("%s\n", (getAns(a[0], a[1], a[2]) ? "Yes" : "No"));
    }
}

#ifdef debug
printf("Time:%.3fs.\n", double(clock() - START) / CLOCKS_PER_SEC);
#endif
return 0;
}

{% endfold %}