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 解题报告的更多相关文章

  1. 洛谷P2877 [USACO07NOV]防晒霜Sunscreen

    题目 此题有多种贪心方法. 首先简化题意: 有几个在数轴上的区间,和几个在数轴上确定的位置的点,问用这些数目的点,最多能满足多少个区间里有点. 注意:此题跟区间选点问题不一样,每个点只能满足一个区间, ...

  2. 洛谷_Cx的故事_解题报告_第四题70

    1.并查集求最小生成树 Code: #include <stdio.h> #include <stdlib.h>   struct node {     long x,y,c; ...

  3. 洛谷 P2317 [HNOI2005]星际贸易 解题报告

    P2317 [HNOI2005]星际贸易 题目描述 输入输出格式 输入格式: 输出格式: 如果可以找到这样的方案,那么输出文件output.txt中包含两个整数X和Y.X表示贸易额,Y表示净利润并且两 ...

  4. 洛谷 P3802 小魔女帕琪 解题报告

    P3802 小魔女帕琪 题目背景 从前有一个聪明的小魔女帕琪,兴趣是狩猎吸血鬼. 帕琪能熟练使用七种属性(金.木.水.火.土.日.月)的魔法,除了能使用这么多种属性魔法外,她还能将两种以上属性组合,从 ...

  5. 洛谷 P2606 [ZJOI2010]排列计数 解题报告

    P2606 [ZJOI2010]排列计数 题目描述 称一个\(1,2,...,N\)的排列\(P_1,P_2...,P_n\)是\(Magic\)的,当且仅当对所以的\(2<=i<=N\) ...

  6. 洛谷1303 A*B Problem 解题报告

    洛谷1303 A*B Problem 本题地址:http://www.luogu.org/problem/show?pid=1303 题目描述 求两数的积. 输入输出格式 输入格式: 两个数 输出格式 ...

  7. 洛谷 P3084 [USACO13OPEN]照片Photo 解题报告

    [USACO13OPEN]照片Photo 题目描述 农夫约翰决定给站在一条线上的\(N(1 \le N \le 200,000)\)头奶牛制作一张全家福照片,\(N\)头奶牛编号\(1\)到\(N\) ...

  8. 洛谷 P1379 八数码难题 解题报告

    P1379 八数码难题 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初 ...

  9. NOIP2015 D2T3 洛谷2680 BZOJ4326 运输计划 解题报告

    前言:个人认为这是历年NOIP中比较简单的最后一题了,因此将自己的思路与大家分享. 题目大意: 给一棵无根树,给出m条路径.允许将树上的一条边的权值改为0.求m条路径长度最大值的最小值.n,m< ...

随机推荐

  1. k-means+python︱scikit-learn中的KMeans聚类实现( + MiniBatchKMeans)

    来源:, init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances='auto', verbose=0, ...

  2. [JSOI2016]病毒感染[dp]

    题意 有 \(n​\) 个村庄按标号排列,每个村庄有一个死亡速度 \(a_i​\) 表示每天死 \(a_i​\) 人(除非你治好这个村庄). 你从 1 号村庄出发,每天可以选择向相邻的村庄进发或者治愈 ...

  3. ML.NET 示例:开篇

    写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...

  4. maven docker 插件集成的几个小坑

    昨晚看springboot视频的时候,发现可以使用docker-maven-plugin这个插件直接build出 docker 镜像到远程服务器上,感觉很方便,于是自己也试了一下,但是碰到了几个问题, ...

  5. item 6: 当auto推导出一个不想要的类型时,使用显式类型初始化的语法

    本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 Item 5解释了比起显式指定类型,使用auto来 ...

  6. 常见 Bash 内置变量介绍

    目录 $0$1, $2 等等$#$* 与 "$*"$@ 与 "$@"$!$_$$$PPID$?$BASH$BASH_VERSION$EUID 与 $UID$GR ...

  7. Bash Shebang 小结

    在 shell(Bash 是一种 shell) 中执行外部程序和脚本时,Linux 内核会启动一个新的进程,以便在新的进程中执行指定的程序或脚本.内核知道该如何为编译型的程序做这件事,但是对于脚本程序 ...

  8. Linux mount 命令

    mount 命令用来挂载文件系统.其基本命令格式为:mount -t type [-o options] device dirdevice:指定要挂载的设备,比如磁盘.光驱等.dir:指定把文件系统挂 ...

  9. youtube下载工具

    Youtube是一个全球性的视频分享网站,其种类之多,内容之丰富,是大家有目共睹的.特别是原创视频更是多不胜数, 每分钟都有400+小时的youtube视频上传,每天都有30亿+的视频被观看.随着视频 ...

  10. D. Cutting Out

    ---恢复内容开始--- 链接 [https://codeforces.com/contest/1077/problem/D] 题意 给你n,k,n个数,找出长度为k,的子串(不需连续),使得该子串数 ...