Bzoj1658 [usaco 2006 Mar] water slides

Source: Internet
Author: User

A good day is coming!

First, I was abused as usaco's silver question... It took two days to complete.

Finally, I found that sort forgot + 1 (start + cnt1 (+ 1) and asked the Administrator for the data...

The practice is not difficult to think about:

(1) divide points into two types: Outbound> inbound> outbound

(2) run the network stream once.

It seems that t is really miserable... Then improve:

It is better to discover that two line segments do not overlap (that is, simply greedy thinking)

As a result, the two types of vertices are sorted, and the matching answer must be the smallest one.

 

 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 }
View code

 

Bzoj1658 [usaco 2006 Mar] water slides

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.