CF1185F Two Pizzas

洛谷评测传送门

题目描述

A company of nn friends wants to order exactly two pizzas. It is known that in total there are 99 pizza ingredients in nature, which are denoted by integers from 11 to 99 .

Each of the nn friends has one or more favorite ingredients: the ii -th of friends has the number of favorite ingredients equal to f_if**i ( 1 \le f_i \le 91≤f**i≤9 ) and your favorite ingredients form the sequence b_{i1}, b_{i2}, \dots, b_{if_i}b**i1,b**i2,…,bif**i ( 1 \le b_{it} \le 91≤bit≤9 ).

The website of CodePizza restaurant has exactly mm ( m \ge 2m≥2 ) pizzas. Each pizza is characterized by a set of r_jr**j ingredients a_{j1}, a_{j2}, \dots, a_{jr_j}a**j1,a**j2,…,ajr**j ( 1 \le r_j \le 91≤r**j≤9 , 1 \le a_{jt} \le 91≤ajt≤9 ) , which are included in it, and its price is c_jc**j .

Help your friends choose exactly two pizzas in such a way as to please the maximum number of people in the company. It is known that a person is pleased with the choice if each of his/her favorite ingredients is in at least one ordered pizza. If there are several ways to choose two pizzas so as to please the maximum number of friends, then choose the one that minimizes the total price of two pizzas.

输入格式

The first line of the input contains two integers nn and mm ( 1 \le n \le 10^5, 2 \le m \le 10^51≤n≤105,2≤m≤105 ) — the number of friends in the company and the number of pizzas, respectively.

Next, the nn lines contain descriptions of favorite ingredients of the friends: the ii -th of them contains the number of favorite ingredients f_if**i ( 1 \le f_i \le 91≤f**i≤9 ) and a sequence of distinct integers b_{i1}, b_{i2}, \dots, b_{if_i}b**i1,b**i2,…,bif**i ( 1 \le b_{it} \le 91≤bit≤9 ).

Next, the mm lines contain pizza descriptions: the jj -th of them contains the integer price of the pizza c_jc**j ( 1 \le c_j \le 10^91≤c**j≤109 ), the number of ingredients r_jr**j ( 1 \le r_j \le 91≤r**j≤9 ) and the ingredients themselves as a sequence of distinct integers a_{j1}, a_{j2}, \dots, a_{jr_j}a**j1,a**j2,…,ajr**j ( 1 \le a_{jt} \le 91≤ajt≤9 ).

输出格式

Output two integers j_1j1 and j_2j2 ( 1 \le j_1,j_2 \le m1≤j1,j2≤m , j_1 \ne j_2j1=j2 ) denoting the indices of two pizzas in the required set. If there are several solutions, output any of them. Pizza indices can be printed in any order.

题意翻译

【题目描述】

现在到了午饭时间,你要为nn个朋友定披萨。

众所周知,披萨的原料分为9种。每位朋友都有自己喜好的原料(一种或多种),第ii个朋友喜欢的原料有f_if**i种,分别是b_{i1},b{i2},...,b{if_i}b**i1,b**i2,...,bif**i

披萨店出售mm种不同的披萨。第jj种披萨里面有r_jr**j种原料,分别是a_{j1},a_{j2},...,a_{jr_j}a**j1,a**j2,...,ajr**j。第jj种披萨售价为c_jc**j

现在你们决定购买恰好两个披萨。我们称一个人是“满意的”,当且仅当他/她想要的每种原料都在至少一个买下来的披萨出现。你希望最多人是“满意的”,并在这个前提下,支出越少越好。请输出任意一种方案。

【输入格式】

第一行,两个正整数n,mn,m,分别代表人数和披萨种数。

接下来nn行,每行描述一个人的口味,先是一个正整数f_if**i,接下来f_if**i个正整数b_{i1},b{i2},...,b{if_i}b**i1,b**i2,...,bif**i,含义如题所示。

接下来mm行,每行描述一种披萨。先是两个正整数c_j,r_jc**j,r**j,接下来r_jr**j个正整数a_{j1},a_{j2},...,a_{jr_j}a**j1,a**j2,...,ajr**j,含义如题所示。

【输出格式】

一行,两个正整数j_1.j_2j1.j2,代表所买的两个披萨。以任意顺序输出任意方案均可。

【数据范围与约定】

保证:

  • 1\leq n\leq 10^5,2\leq m\leq 10^51≤n≤105,2≤m≤105
  • 1\leq c_j\leq 10^91≤c**j≤109
  • 1\leq f_i,b_{it},r_j,a_{jt}\leq 91≤f**i,bit,r**j,ajt≤9

输入输出样例

输入 #1复制

输出 #1复制

输入 #2复制

输出 #2复制

输入 #3复制

输出 #3复制

题解:

2019.11.8模拟赛T2 30分场

前一天刚学状压就来一道状压的题,虽然没场上切但还是感谢出题人@ysy20021208,吸一口大佬欧气。

一开始看到数据范围果断选择了状压,一看就是这9种东西选还是没选,然后觉得可以依次枚举这\(m\)张披萨两两搭配合并之后能满足多少人的欲望。进行更新。一看这复杂度是\(O(N*M*M)\)的,肯定是废只能拿30分。后来一想:虽然\(n\)是\(10^5\)级别的,但是这\(9\)位的状态无论如何只能搭配出\(512(2^9)\)种可能,所以\(N\)就被我们优化到了\(512\),然后一顿乱搞,最后还是拿了30分(滑稽)

(暴力考场代码使用了各种\(C++STL\)...)

#include<cstdio>
#include<bitset>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=1e5+10;
const int INF=2147483645;
int n,m,maxx,minn=INF,ansx,ansy,tot;
bitset<10> t;
bitset<10> s[1<<9+1];
vector<pair<int,bitset<10> > >vec;
struct node
{
int v,id;
bitset<10> x;
}p[maxn];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
int a,f;
bitset<10> tt;
scanf("%d",&a);
for(int j=1;j<=a;j++)
{
int k;
scanf("%d",&k);
tt.set(k);
}
for(int j=0;j<vec.size();j++)
if(vec[j].second==tt)
{
vec[j].first++;
break;
}
vec.push_back(make_pair(1,tt));
}
for(int i=1;i<=m;i++)
{
int a;
scanf("%d%d",&p[i].v,&a);
for(int j=1;j<=a;j++)
{
int k;
scanf("%d",&k);
p[i].x.set(k);
}
p[i].id=i;
}
maxx=-1;
for(int i=1;i<=m;i++)
for(int j=i;j<=m;j++)
{
tot=0;
if(i==j)
continue;
t=(p[i].x|p[j].x);
for(int k=0;k<vec.size();k++)
if((vec[k].second&t)==vec[k].second)
tot+=vec[k].first;
if(tot>=maxx)
{
if(tot>maxx)
minn=INF;
maxx=tot;
if((p[i].v+p[j].v)<minn)
{
ansx=i,ansy=j;
minn=p[i].v+p[j].v;
}
}
}
printf("%d %d",ansx,ansy);
return 0;
}

然后我详细介绍一下看完题解之后自己捋顺的满分思路:

其实暴力想的时候已经离正解好近了(真是遗憾)。

我想到了把\(n\)个人的状态进行包含性质的枚举,但是就是没想到\(m\)张披萨也可以这么干(简直匪夷所思)。

看一下题:我们发现:对于两张披萨,它们有可能能满足同一个人的需求,这时,这两张披萨之间肯定是子集或真子集的关系。那么我们完全可以把\(m\)张披萨也“需求化”。什么意思呢?既然枚举所有的\(n,m\)复杂度过高,那么我们就“要什么拿什么”,设置\(minn[st]\)数组来存能满足需求为\(st\)的人的所有披萨中价值最小的那张的花费。并且,存储\(num[st]\)表示需求为\(st\)的人的人数。同时,因为我们需要选择最优秀的两张披萨,所以还需要记录一下满足需求为\(st\)的人的所有披萨中价值第二小的花费为\(minn2[st]\)。

那么我们在对披萨们进行预处理的时候,就可以顺道把这两个数组处理出来。并且,因为我们最终要输出披萨的编号,所以我们还需要另开数组\(k,k2\)来存当前状态最小价值披萨的编号、次小价值披萨的编号。

然后我们就完成了暴力的优化。由原来的\(O(m^2)\)级别的复杂度成功降低到了\(O(512\times 512)\)的复杂度。所以,在我们状态压缩的时候,懂得优化复杂度,一定要从状态入手,看枚举的东西能不能按状态进一步压缩而变得更快。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e5+10;
const int maxs=1<<9;
int n,m;
int a[maxn],b[maxn],v[maxn],num[maxs];
int minn[maxs],minn2[maxs],k[maxs],k2[maxs];
int ans[maxs],ansx[maxs],ansy[maxs];
int main()
{
memset(minn,0x3f,sizeof(minn));
memset(minn2,0x3f,sizeof(minn2));
memset(ans,0x7f,sizeof(ans));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
int p;
scanf("%d",&p);
for(int j=1;j<=p;j++)
{
int k;
scanf("%d",&k);
a[i]|=1<<(k-1);
}
num[a[i]]++;
}
for(int i=1;i<=m;i++)
{
int p;
scanf("%d%d",&v[i],&p);
for(int j=1;j<=p;j++)
{
int k;
scanf("%d",&k);
b[i]|=1<<(k-1);
}
if(v[i]<minn[b[i]])
{
minn2[b[i]]=minn[b[i]];
minn[b[i]]=v[i];
k2[b[i]]=k[b[i]];
k[b[i]]=i;
}
else if(v[i]<minn2[b[i]])
{
minn2[b[i]]=v[i];
k2[b[i]]=i;
}
}
for(int i=0;i<maxs;i++)
if(k[i]&&k2[i])
{
ans[i]=minn[i]+minn2[i];
ansx[i]=k2[i],ansy[i]=k[i];
}
for(int i=0;i<maxs;i++)
for(int j=i+1;j<maxs;j++)
{
if(minn[i]>1000000000 || minn[j]>1000000000)
continue;
ans[i|j]=min(ans[i|j],minn[i]+minn[j]);
if(ans[i|j]==minn[i]+minn[j])
{
ansx[i|j]=k[i];
ansy[i|j]=k[j];
}
}
for(int i=0;i<maxs;i++)
printf("%d\n",ans[i]);
int target=0,maxx=-1,mi=2100000000;
for(int i=0;i<maxs;i++)
{
if(ans[i]>2000000000)
continue;
int tmp=0;
for(int j=0;j<maxs;j++)
if((j&i)==j)
tmp+=num[j];
if(tmp>maxx)
target=i,maxx=tmp,mi=ans[i];
else if(tmp==maxx && ans[i]<mi)
target=i,mi=ans[i];
}
printf("%d %d\n",ansx[target],ansy[target]);
return 0;
}

CF1185F Two Pizzas的更多相关文章

  1. CF1185F Two Pizzas 状压

    你发现 pizza 种类数不会很多,状压一下就可以了 code: #include <bits/stdc++.h> #define M 11 #define N 100005 #defin ...

  2. Django模型类Meta元数据详解

    转自:https://my.oschina.net/liuyuantao/blog/751337 简介 使用内部的class Meta 定义模型的元数据,例如: from django.db impo ...

  3. ANNOTATION PROCESSING 101 by Hannes Dorfmann — 10 Jan 2015

    原文地址:http://hannesdorfmann.com/annotation-processing/annotationprocessing101 In this blog entry I wo ...

  4. django model Meta选项

    可用的 Meta 选项 abstract Options.abstract 如果 abstract = True ,这个 model 就是一个 抽象基类 . app_label Options.app ...

  5. What Is Mathematics?

    What Is Mathematics? The National Council of Teachers of Mathematics (NCTM), the world's largest org ...

  6. poj3311 Hie with the Pie (状态压缩dp,旅行商)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3160   Accepted: 1613 ...

  7. 状态压缩 DP

    D - Hie with the Pie Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536 ...

  8. 【Django】Django model与数据库操作对应关系(转)

    Django对数据库的操作分用到三个类:Manager.QuerySet.Model. Manager的主要功能定义表级方法(表级方法就是影响一条或多条记录的方法),我们可以以models.Manag ...

  9. Head First Design Patterns

    From Head First Design Patterns. Design Principle: Idnetify the aspects of your application that var ...

随机推荐

  1. 201871010102-常龙龙《面向对象程序设计(java)》第十四周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  2. LG5202 「USACO2019JAN」Redistricting 动态规划+堆/单调队列优化

    问题描述 LG5202 题解 \[opt[i]=xx+(cnt[i]-cnt[yy]<=0)\] 发现\(cnt[i]-cnt[yy] <= 0\)只能有两种取值 于是直接堆优化即可 \( ...

  3. Eclipse引入自定义XML约束文件(DTD,SCHEMA)问题

    Eclipse引入自定义XML约束文件(DTD,SCHEMA)问题 1:说明 使用Eclipse 编写xml文件的约束文件的,包括DTD约束文件,Schema约束文件的时候, 我们也需要接受eclip ...

  4. MongoDB for OPS 04:备份恢复

    写在前面的话 和 MySQL 一样,mongodb 也是需要将数据进行备份的,毕竟天有不测风云,谁也不知道哪天机器就炸了. 备份恢复 mongodb 提供了两种备份恢复手段:mongoexport / ...

  5. layui 使用随记

    layui confir使用 不显示右上角关闭按钮 针对提示框内按钮指定操作 layer.confirm("这里填写提示信息", {closeBtn:0,icon: 0, titl ...

  6. python将oracle数据库保存到excel

    上代码: import pandas as pd import SqlHelper.ORACLE as ORA if __name__ == '__main__': #连接数据库 ms = ORA.O ...

  7. 「SAP技术」SAP MM 事务代码ME17的用法

    SAP MM 事务代码ME17的用法 1,如下采购信息记录需要被归档: PIR号码,5300007816   2, ME12打上删除标记,   3, 事务代码ME17做归档 3.1 创建archive ...

  8. 如何编写 maptalks plugin

    前面写过 maptalks plugin ( ArcGISTileLayer ),有读者留言说文章写得太精简,根据文章给出的核心代码没办法写出一个完整的 plugin ( 文中有完整 demo 地址, ...

  9. 开源项目Telegram源码 Telegram for Android Source

    背景介绍 Telegram 是一款跨平台的即时通信软件,它的客户端是自由及开放源代码软件.用户可以相互交换加密与自毁消息,发送照片.影片等所有类型文件.官方提供手机版.桌面版和网页版等多种平台客户端. ...

  10. WC 个人项目 ( node.js 实现 )

    基于 node.js 的 wordCounter 个人项目 GitHub 项目地址:https://github.com/KofeChen/node.js-WordCounter 实现功能: 能够匹配 ...