hdu1050&&nyoj220 moving Tables (greedy)

Source: Internet
Author: User

Moving TablesTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 24372 Accepted Submission (s): 8071


Problem DescriptionThe Famous ACM (Advanced computer Maker) company have rented a floor of a building whose shape was in the Following figure.



The floor has a rooms each of the side and South side along the corridor. Recently the company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables was big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out of the following plan:moving a table from a hostel to another the can is done within minutes. When moving a table from the class I to the Class J, the part of the corridor between the front of the class I and the front of the class J is Used. So, during each minutes, several moving between and rooms not sharing the same part of the corridor would be done Simult aneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving.



For each of the either, at the most one table would be a moved in or moved out. Now, the manager seeks out a method to minimize the time to move all the tables. Your job is-to-write a program to solve the manager ' s problem.

Inputthe input consists of T test cases. The number of test cases) (T is given in the first line of the input. Each test case begins with a line containing an integer N, 1<=n<=200, which represents the number of tables to move . Each of the following N lines contains-positive integers s and t, representing that a table was to move from the hostel numbe R S to the guest number T (each of the number appears at a once in the N lines). From the N+3-rd line, the remaining test cases is listed in the same manner as above.

Outputthe output should contain the minimum time in minutes to complete the moving, one per line.

Sample Input

Sample Output
102030

Sourceasia 2001, Taejon (South Korea)
Recommendwe carefully selected several similar problems for you:1052 1789 1045 1053 1800
Statistic | Submit | Discuss | Note

Very classic greedy. I remember seeing it many times before, but I didn't do it every time.

Unfortunately, it's not the past. Haha 、、、

Similar to intercepting missiles, however, the difference between this and interceptor missiles is that there are two rooms in a corridor, and if a room is in a corridor, the opposite room cannot use the corridor.

Just a little bit of optimization, please. Put all the odd rooms all +1, without affecting the result.

#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;struct node{int A, B;} C[205];int Vis[205];bool CMP (node X,node y) {if (X.A<Y.A) return true;if (x.a==y.a&&x.b<y.b) return true; return false;} int main () {int ncase,n;scanf ("%d", &ncase), while (ncase--) {scanf ("%d", &n), memset (vis,0,sizeof (VIS)); int i=0;i<n;i++) {int x,y,temp;scanf ("%d%d", &x,&y), if (x%2) x++;if (y%2) y++;if (x>y) temp=x,x=y,y=temp;/ /careful, c[i].a=x,c[i].b=y;} Sort (c,c+n,cmp); int count=0,mark;for (int i=0;i<n;i++)//similar to interceptor missiles. is the minimum number of interceptions. {if (!vis[i]) {count++,mark=c[i].b;for (int j=i+1;j<n;j++) if (!vis[j]&&c[j].a>mark) vis[j]=1 , mark=c[j].b;}} printf ("%d\n", count*10);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1050&&nyoj220 moving Tables (greedy)

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.