Cunning Gena CodeForces - 417D

Cunning Gena CodeForces - 417D

题意

先将小伙伴按需要的监视器数量排序。然后ans[i][j]表示前i个小伙伴完成j集合内题目所需最少钱。那么按顺序枚举小伙伴,用ans[i-1][j]更新ans[i][j]和ans[i][j | 第i个小伙伴能完成题目的集合](更新后一种时答案要加上第i个小伙伴的费用)。

由于小伙伴已经按监视器数量排序了,对于每个枚举出的小伙伴i,可以试着将其作为最后一个要求助的小伙伴,那么此时监视器上花的钱就是这个小伙伴所需监视器数量*每个的费用,求助小伙伴费用就是ans[i][所有题目的集合],此时总费用就是这两者的总和。最终答案就是最小的枚举得到的总费用。

可以开滚动数组优化空间。

错误记录:45-47无效代码导致TLE

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 using namespace std;
 6 typedef long long LL;
 7 LL n,m1,b;
 8 struct A
 9 {
10     LL x,k,v;
11     bool operator<(const A& b) const
12     {
13         return k<b.k;
14     }
15 }aa[110];
16 LL ans[2][1048600];
17 LL anss;
18 int main()
19 {
20     LL i,j,k,t,tt,ii=0;
21     scanf("%I64d%I64d%I64d",&n,&m1,&b);
22     for(i=1;i<=n;i++)
23     {
24         scanf("%I64d%I64d%I64d",&aa[i].x,&aa[i].k,&tt);
25         for(j=1;j<=tt;j++)
26         {
27             scanf("%I64d",&t);
28             //v[i].push_back(t);
29             aa[i].v|=(1<<(t-1));
30         }
31     }
32     sort(aa+1,aa+n+1);
33     anss=0x3f3f3f3f3f3f3f3f;
34     memset(ans,0x3f,sizeof(ans));
35     ans[0][0]=0;
36     for(i=0;i<n;i++)
37     {
38         ii^=1;
39         memset(ans[ii],0x3f,sizeof(ans[ii]));
40         for(j=0;j<(1<<m1);j++)
41         {
42             ans[ii][j]=min(ans[ii][j],ans[ii^1][j]);
43             ans[ii][j|aa[i+1].v]=min(ans[ii][j|aa[i+1].v],ans[ii^1][j]+aa[i+1].x);
44         }
45         /*for(j=0;j<(1<<m1);j++)
46             for(k=0;k<m1;k++)
47                 ans[ii][j]=min(ans[ii][j],ans[ii][j|(1<<k)]);*/
48         anss=min(anss,ans[ii][(1<<m1)-1]+b*aa[i+1].k);
49     }
50     if(anss!=0x3f3f3f3f3f3f3f3f)
51         printf("%I64d",anss);
52     else
53         printf("-1");
54     return 0;
55 }
时间: 2024-08-03 08:43:20

Cunning Gena CodeForces - 417D的相关文章

Codeforces 417D Cunning Gena(状态压缩dp)

题目链接:Codeforces 417D Cunning Gena 题目大意:n个小伙伴.m道题目,每一个监视器b花费,给出n个小伙伴的佣金,所须要的监视器数,以及能够完毕的题目序号. 注意,这里仅仅要你拥有的监视器数量大于小伙伴须要的监视器数量就可以. 求最少花费多少金额能够解决全部问题. 解题思路:dp[i],i为一个二进制数.表示完毕这些题目的最小代价,可是这里要注意,由于有个监视器的数量.普通情况下要开一个二维的状态.可是2^20次方有一百万,再多一维的数组会超内存,所以我的做法是将每一

COdeforces#417D Cunning Gena(状压DP)

A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him. The participants

codeforce 417D Cunning Gena (状压DP)

原题地址:http://codeforces.com/problemset/problem/417/D 题意: Gena 为了解决m个问题,请他的朋友们帮忙,其中第i个朋友可以解决某mi个问题,需要花费xi卢布,并且要求安装至少ki个显示器,每个显示器需要b卢布. 问解决所有问题需要多少多少卢布 题解 考虑问题数目很小,可以状压DP. dp[S]表示当前状态的最优解,通过位运算进行转移. 考虑|没有逆运算,所以只能正推更新. 首先把朋友按照k排序,避免k的干扰. dp[][0]和dp[][1]分

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

CodeForces 614B Gena&#39;s Code

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <math.h> using namespace std; int flag; char s[100000+10]; int zero; char q[100000+10]; bool Perfect() { int len=strlen(s); for(int i=1;

[CodeForces - 614B] B - Gena&#39;s Code

A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the exposeprocedure. Unfortunately, Rostislav is unable to understand the definition

codeforces Gym 100187B B. A Lot of Joy

B. A Lot of Joy Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/A Description Two boys Gena and Petya wrote on two strips of paper the same string s that consisted of lowercase Latin letters. Then each boy took o