BZOJ1658 [Usaco2006 Mar]Water Slides 滑水

说好的一天题解来啦!

首先作为usaco的silver题,我被虐了。。。调了两天才搞定

最后发现是sort忘了+1(start + cnt1 (+ 1))还去问管理员要了数据,真是。。。

做法倒不是很难想:

(1)把点分成出度>入度和入度>出度两种

(2)跑一遍网络流

好像会T的很惨。。。然后改进:

发现两条线段交叉一定没有不交叉来得好(就是简单地贪心思想)

于是两类点排序,直接一个一个配对答案一定是最小的。

 1 /**************************************************************
 2     Problem: 1658
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:32 ms
 7     Memory:1040 kb
 8 ****************************************************************/
 9
10 #include <cstdio>
11 #include <algorithm>
12
13 using namespace std;
14
15 int n, m, ans, cnt1, cnt2, X, Y;
16 int deg[15000], start[15000], stop[15000], pos[15000];
17
18 int main(){
19     scanf("%d%d", &n, &m);
20     for (int i = 1; i <= n; ++i)
21         scanf("%d", pos + i);
22     for (int i = 1; i <= m; ++i){
23         scanf("%d%d", &X, &Y);
24         ++deg[X], --deg[Y];
25     }
26
27     for (int i = 1; i <= n; ++i){
28         for(; deg[i] > 0; --deg[i]) start[++cnt1] = pos[i];
29         for(; deg[i] < 0; ++deg[i]) stop[++cnt2] = pos[i];
30     }
31
32     sort(start + 1, start + cnt1 + 1);
33     sort(stop + 1, stop + cnt2 + 1);
34
35     for (int i = 1; i <= cnt1; ++i)
36         ans += abs(start[i] - stop[i]);
37     printf("%d\n", ans);
38     return 0;
39 }

时间: 2024-10-13 20:28:31

BZOJ1658 [Usaco2006 Mar]Water Slides 滑水的相关文章

bzoj 1658: [Usaco2006 Mar]Water Slides 滑水

题解: 很神奇的做法,把点分成入度大于出度和入度小于出度两种. 然后入度大于出度的点必须走到某个点,所以排序贪心. #include<stdio.h> #include<iostream> #include<algorithm> #include<stdlib.h> using namespace std; const int N=10005; int n,m,i,ans,x,y,k,s,a[N],p[N],b[N],c[N]; inline void re

BZOJ 1658 Water Slides 滑水

Water Slides 滑水 [问题描述] It's a hot summer day, and Farmer John is letting Betsy go to the water park where she intends to ride every single slide. The water park has N (1 <= N <= 10,000) platforms (numbered 1..N) from which to enter the M (1 <= M

[BZOJ1657][Usaco2006 Mar]Mooo 奶牛的歌声

1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 863  Solved: 607 [Submit][Status][Discuss] Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique heigh

BZOJ 1657: [Usaco2006 Mar]Mooo 奶牛的歌声( 单调栈 )

从左往右,从右往左 扫两遍,单调栈维护... ---------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<stack> #include<iostream> #define rep( i , n ) for( int i = 0 ; i &

1657: [Usaco2006 Mar]Mooo 奶牛的歌声

1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 526  Solved: 365[Submit][Status] Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique height h in the

[BZOJ] 1657: [Usaco2006 Mar]Mooo 奶牛的歌声

1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 858  Solved: 603[Submit][Status][Discuss] Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique height

[BZOJ1657] [Usaco2006 Mar] Mooo 奶牛的歌声 (单调栈)

Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique height h in the range 1..2,000,000,000 nanometers (FJ really is a stickler for precision). Each cow moos at some volume v in th

[Luogu2991][USACO10OPEN]水滑梯Water Slides

题面戳我 题面描述 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建一个水上乐园.当然,它最大的亮点就是新奇巨大的水上冲浪. 超级轨道包含 E (1 <= E <=150,000)条小轨道连接着V (V <= 50,000)个水池,编号为1..V.每个小轨道必须按照特定的方向运行,不能够反向运行.奶牛们从1号水池出发,经过若干条小轨道,最终到达V号水池.每个水池(除了V号和1号之外,都有至少一条小轨道进来和一条小轨道出去,并且,一头奶牛从任何一个水池到达V号

[Usaco2006 Mar]Mooo 奶牛的歌声题解

题目链接 bzoj1657 思路: 本题可用单调栈思想来解.维护一个单调下降的序列,由于是下降的,所以栈里的每一头牛的声音都不能被后面的牛听到.当一个身高较大的牛加入栈时,栈不再单调,需要踢掉一些牛,而那些牛的身高都比它矮所以可以把那些牛的声音加到当前牛的伤残值里.而由于那些较矮的牛的声音会被阻挡,所以踢掉不会影响后续答案. 由于声音是双向传播的,所以需要正序做一遍,再逆序做一遍. code: #include<cstdio> #include<iostream> #include