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