Noip Demo match//greedy strategy

Source: Internet
Author: User

Match (match.pas/match.c/match.cpp)

[ title Description ]

By the new semester,McxThe painful discovery general technology class actually has the experiment class, in this way he will have to abandon the writing profession the idea to do a similar to build the building block the thing. In an experimental class, he found that the material had many long blocks, of which the yellow one hadNArticle, sectionIThe length of the bar isAi; blue one.mArticle, sectionJThe length of the bar isBj。 So he thought: how many of these blocks can be formed on the guide rail? Each pair of rails consists of a yellow block and a blue building block, each of which can only be used once. For aesthetics, when and only ifAi–x <= Bj <= Ai + y, two blocks can form a pair of rails.x, yis a given non-negative integer.

[ topic input ]

The first line four number n,m,x,y, the concrete meaning see the topic description.

The second row n number, number I indicates the length of the yellow block of article I , there is a space between every two numbers.

The number of the third row m , the number of I indicates the length of the blue block of article I , there is a space between every two numbers.

[ topic output ]

A single, non-negative integer that represents the maximum number of rails that can be composed.

[ Sample input ]

5 3 0 0

1 2 3) 3 4

1 3 5

[ sample output ]

2

[ Sample explanation ]

In the sample, x, y are 0, so only a pair of rails can be formed when ai=bj .

The scheme is a group of the first yellow block and the first blue building block, the second blue building block and the third or four yellow bricks.

[ data range ]

50% Data meets 1<= N, M <=

100% data satisfies 1<= N, M <= 100000

100% Data meets 0<= x, y, Ai, Bi <= 10^9

100% data is met,Ai and Bi must be in ascending order.

Analysis:

Get this problem, in fact, the first can be thought of if the data is small, with the Hungarian algorithm can be run down. But unfortunately, I won't. Consider other ways to find out if we are blind to basic greed. Seems to be able to do. And then, what if the blind basic greed? Obviously we're here if we choose to meet the minimum value within the required interval each time. Because the sequence becomes monotonically increasing. Then we can guarantee that each time the first match is made.

Suppose Ai<=aj also has bi<bj. and ensure that both AI AJ and bi Bj can meet each other's requirements. So if we match AI with BJ AJ and bi in this way it's exactly the same way that AI and bi AJ and BJ match each other. Then we can choose the order of the second way to ensure the best. (If the conditions here change AI does not match BJ, then the second approach is also optimal.) If the conditions here change if the AI and AJ can only match with BI, then the two can only choose one) so if we start from scratch the basic greedy is to guarantee the optimal solution.

Release code: Here to pay attention to the boundary judgment and timely termination of the cycle.

#include <cstdio> #include <algorithm>using namespace Std;int a[100010],b[100010],n,m,x,y,ans;int main () { Freopen ("Match.in", "R", stdin); Freopen ("Match.out", "w", stdout); scanf ("%d%d%d%d", &n,&m,&x,&y); for (int. i=1;i<=n;++i) scanf ("%d", &a[i]), for (int i=1;i<=m;++i) scanf ("%d", &b[i]); int head;for (int i=1;i <=m;++i) {if (b[i]>=a[1]-x) {head=i;break;}} int I=1;while (i<=n) {while (1) {if (b[head]<a[i]-x) {++head, if (head>m) break; continue;} if (b[head]>a[i]+y) break;++ans;++head;break;} ++i;if (head>m) break; printf ("%d\n", ans); fclose (stdin); fclose (stdout); return 0;}

Noip Demo match//greedy strategy

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.