洛谷 P2887 [USACO07NOV]防晒霜Sunscreen 解题报告
P2887 [USACO07NOV]防晒霜Sunscreen
题目描述
To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they're at the beach. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000) that will work. If the SPF rating is too low, the cow suffers sunburn; if the SPF rating is too high, the cow doesn't tan at all........
The cows have a picnic basket with L (1 ≤ L ≤ 2500) bottles of sunscreen lotion, each bottle i with an SPF rating SPFi (1 ≤ SPFi ≤ 1,000). Lotion bottle i can cover coveri cows with lotion. A cow may lotion from only one bottle.
What is the maximum number of cows that can protect themselves while tanning given the available lotions?
有\(C\)个奶牛去晒太阳 (1 <=\(C\) <= 2500),每个奶牛各自能够忍受的阳光强度有一个最小值和一个最大值,太大就晒伤了,太小奶牛没感觉。
而刚开始的阳光的强度非常大,奶牛都承受不住,然后奶牛就得涂抹防晒霜,防晒霜的作用是让阳光照在身上的阳光强度固定为某个值。
那么为了不让奶牛烫伤,又不会没有效果。
给出了\(L\)种防晒霜。每种的数量和固定的阳光强度也给出来了
每个奶牛只能抹一瓶防晒霜,最后问能够享受晒太阳的奶牛有几个。
输入输出格式
输入格式:
Line 1: Two space-separated integers: \(C\) and \(L\)
Lines 2..\(C+1\): Line i describes cow i's lotion requires with two integers: \(minSPFi\) and \(maxSPFi\)
Lines \(C+2\)..\(C+L+1\): Line \(i+C+1\) describes a sunscreen lotion bottle \(i\) with space-separated integers: \(SPFi\) and \(coveri\)
输出格式:
A single line with an integer that is the maximum number of cows that can be protected while tanning
\(DINIC\)周常写爆:(1/1)
思路:s连每头牛边1,每头牛连合法的防晒霜边1,防晒霜连t使用次数,跑最大流即可。(此题也有别的解法)
前后代码对比:
54分:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int min(int x,int y){return x<y?x:y;}
const int N=2508;
const int inf=0x3f3f3f3f;
int C,L,l[N],r[N],T;
struct Edge
{
int next,to,w;
}edge[N*N];
int head[N<<2],cnt=-1;
void add(int u,int v,int w)
{
edge[++cnt].next=head[u];edge[cnt].to=v;edge[cnt].w=w;head[u]=cnt;
}
int dep[N<<2],s[N<<2],pre[N<<2],used[N<<2],tot=0,ans=0;
queue <int > q;
void push(int x){s[++tot]=x;}
void pop(){tot--;}
bool bfs()
{
memset(dep,0,sizeof(dep));
while(!q.empty()) q.pop();
q.push(0);
dep[0]=1;
while(!q.empty()&&q.front()!=T)
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to,w=edge[i].w;
if(!dep[v]&&w)
{
dep[v]=dep[u]+1;
q.push(v);
}
}
}
return !q.empty();
}
int main()
{
scanf("%d%d",&C,&L);
memset(head,-1,sizeof(head));
T=C+L+1;
for(int i=1;i<=C;i++)
{
scanf("%d%d",l+i,r+i);
add(0,i,1),add(i,0,0);
}
int num,d;
for(int i=1;i<=L;i++)
{
scanf("%d%d",&d,&num);
for(int j=1;j<=C;j++)
if(l[j]<=d&&d<=r[j])
add(j,i+C,1),add(i+C,j,0);
add(i+C,T,num),add(T,i+C,0);
}
while(bfs())
{
push(0);
memset(used,0,sizeof(used));
memset(pre,0,sizeof(pre));
while(tot)
{
if(s[tot]==T)
{
int now=tot,id,m_min=inf;
while(pre[s[now]])
{
if(m_min>=edge[pre[s[now]]].w)
{
m_min=edge[pre[s[now]]].w;
id=now-1;
}
now--;
}
ans+=m_min;now=tot;
while(pre[s[now]])
{
edge[pre[s[now]]].w-=m_min;
edge[pre[s[now]]^1].w+=m_min;
now--;
}
tot=id;
used[T]=0;
}
else
{
int u=s[tot];
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to,w=edge[i].w;
if(!used[v]&&dep[v]==dep[u]+1&&w)
{
push(v);
pre[v]=i;
used[v]=1;
break;
}
}
if(s[tot]==u) pop();
}
}
}
printf("%d\n",ans);
return 0;
}
100分:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int min(int x,int y){return x<y?x:y;}
const int N=2508;
const int inf=0x3f3f3f3f;
int C,L,l[N],r[N],T;
struct Edge
{
int next,to,w;
}edge[N*N];
int head[N<<2],cnt=-1;
void add(int u,int v,int w)
{
edge[++cnt].next=head[u];edge[cnt].to=v;edge[cnt].w=w;head[u]=cnt;
}
int dep[N<<2],s[N<<2],pre[N<<2],used[N<<2],tot=0,ans=0;
queue <int > q;
void push(int x){s[++tot]=x;}
void pop(){tot--;}
bool bfs()
{
memset(dep,0,sizeof(dep));
while(!q.empty()) q.pop();
q.push(0);
dep[0]=1;
while(!q.empty()&&q.front()!=T)
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to,w=edge[i].w;
if(!dep[v]&&w)
{
dep[v]=dep[u]+1;
q.push(v);
}
}
}
return !q.empty();
}
int main()
{
scanf("%d%d",&C,&L);
memset(head,-1,sizeof(head));
T=C+L+1;
for(int i=1;i<=C;i++)
{
scanf("%d%d",l+i,r+i);
add(0,i,1),add(i,0,0);
}
int num,d;
for(int i=1;i<=L;i++)
{
scanf("%d%d",&d,&num);
for(int j=1;j<=C;j++)
if(l[j]<=d&&d<=r[j])
add(j,i+C,1),add(i+C,j,0);
add(i+C,T,num),add(T,i+C,0);
}
while(bfs())
{
push(0);
memset(used,0,sizeof(used));
memset(pre,-1,sizeof(pre));
while(tot)
{
if(s[tot]==T)
{
int now=tot,id,m_min=inf;
while(pre[s[now]]!=-1)
{
if(m_min>=edge[pre[s[now]]].w)
{
m_min=edge[pre[s[now]]].w;
id=now-1;
}
now--;
}
ans+=m_min;now=tot;
while(pre[s[now]]!=-1)
{
edge[pre[s[now]]].w-=m_min;
edge[pre[s[now]]^1].w+=m_min;
now--;
}
tot=id;
used[T]=0;
}
else
{
int u=s[tot];
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to,w=edge[i].w;
if(!used[v]&&dep[v]==dep[u]+1&&w)
{
push(v);
pre[v]=i;
used[v]=1;
break;
}
}
if(s[tot]==u) pop();
}
}
}
printf("%d\n",ans);
return 0;
}
0和-1的锅。。。
2018.6.8
洛谷 P2887 [USACO07NOV]防晒霜Sunscreen 解题报告的更多相关文章
- 洛谷P2877 [USACO07NOV]防晒霜Sunscreen
题目 此题有多种贪心方法. 首先简化题意: 有几个在数轴上的区间,和几个在数轴上确定的位置的点,问用这些数目的点,最多能满足多少个区间里有点. 注意:此题跟区间选点问题不一样,每个点只能满足一个区间, ...
- 洛谷_Cx的故事_解题报告_第四题70
1.并查集求最小生成树 Code: #include <stdio.h> #include <stdlib.h> struct node { long x,y,c; ...
- 洛谷 P2317 [HNOI2005]星际贸易 解题报告
P2317 [HNOI2005]星际贸易 题目描述 输入输出格式 输入格式: 输出格式: 如果可以找到这样的方案,那么输出文件output.txt中包含两个整数X和Y.X表示贸易额,Y表示净利润并且两 ...
- 洛谷 P3802 小魔女帕琪 解题报告
P3802 小魔女帕琪 题目背景 从前有一个聪明的小魔女帕琪,兴趣是狩猎吸血鬼. 帕琪能熟练使用七种属性(金.木.水.火.土.日.月)的魔法,除了能使用这么多种属性魔法外,她还能将两种以上属性组合,从 ...
- 洛谷 P2606 [ZJOI2010]排列计数 解题报告
P2606 [ZJOI2010]排列计数 题目描述 称一个\(1,2,...,N\)的排列\(P_1,P_2...,P_n\)是\(Magic\)的,当且仅当对所以的\(2<=i<=N\) ...
- 洛谷1303 A*B Problem 解题报告
洛谷1303 A*B Problem 本题地址:http://www.luogu.org/problem/show?pid=1303 题目描述 求两数的积. 输入输出格式 输入格式: 两个数 输出格式 ...
- 洛谷 P3084 [USACO13OPEN]照片Photo 解题报告
[USACO13OPEN]照片Photo 题目描述 农夫约翰决定给站在一条线上的\(N(1 \le N \le 200,000)\)头奶牛制作一张全家福照片,\(N\)头奶牛编号\(1\)到\(N\) ...
- 洛谷 P1379 八数码难题 解题报告
P1379 八数码难题 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初 ...
- NOIP2015 D2T3 洛谷2680 BZOJ4326 运输计划 解题报告
前言:个人认为这是历年NOIP中比较简单的最后一题了,因此将自己的思路与大家分享. 题目大意: 给一棵无根树,给出m条路径.允许将树上的一条边的权值改为0.求m条路径长度最大值的最小值.n,m< ...
随机推荐
- 省市区三级联动,JS实现
文件下载地址:http://files.cnblogs.com/files/chenwolong/jsAddress.rar 示例截图: 在这里自己记录一个方法: function cmbAddOpt ...
- 微信小程序日常开发中常遇到的错误代码
在开发过程中,会遇到很多微信返回的状态码,鬼知道代表什么意思,现在好了,整理总结了一份状态码,方便大家. 微信小程序错误码参考 状态码(场景值) 说明 -1 系统繁忙 0 请求成功 40001 ...
- 解读tensorflow之rnn
from: http://lan2720.github.io/2016/07/16/%E8%A7%A3%E8%AF%BBtensorflow%E4%B9%8Brnn/ 这两天想搞清楚用tensorfl ...
- 使用xshell连接服务器,数字键盘无法使用解决办法
打开会话管理器,选中需要设置的服务器连接,右键->属性 选中 终端->VT模式->初始数字键盘模式->设为普通 保存,重新连接即可.
- 五年.net程序员Java学习之路
大学毕业后笔者进入一家外企,做企业CRM系统开发,那时候开发效率最高的高级程序语言,毫无疑问是C#.恰逢公司也在扩张,招聘了不少.net程序员,笔者作为应届生,也乐呵呵的加入到.net程序员行列中. ...
- python2.6升级到3.3.0 以及依赖库在迁移时的处理
线上服务器python版本默认是2.6,由于业务程序要求,需要将python升级到3.3.0, 操作记录如下: Cenots6.8默认安装的是2.6版本,要更新升级需安装下gcc: [root@ope ...
- C. A Mist of Florescence
链接 [http://codeforces.com/contest/989/problem/C] 题意 给定A B C D四个字符个数,让你构造一个矩阵使得他们的个数恰好那么多,联通块算一块 分析 构 ...
- scenario testing
我们的APP“吃了么”是专为爱美食的人打造的,典型的用户自然是那些喜欢美食的“吃货”们,当然也可以为想要快速找到周边餐馆的童鞋提供便利.还有一种典型的用户就是喜欢自己烹调食物的人. 我们整理出来了下面 ...
- 用软件工程分析开源项目octave的移植
在本科的时候学习了软件工程,报考了信息系统项目管理师的考试,,虽然没有过,但是其实还是学了一些相关项目管理方面的知识,,9大管理,,当年应该能背出来,,, 1 项目整体管理 2 项目范围管理 3 项目 ...
- [转] Linux有问必答:如何修复“sshd error: could not load host key”
编译自:http://ask.xmodulo.com/sshd-error-could-not-load-host-key.html作者: GOLinux 本文地址:https://linux.cn/ ...