POJ1083Moving Tables

View Code
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

typedef struct move
{
    int from;
    int to;
} move;

int main()
{
    move m[400];
    int shap[400];
    int T, N;
    int x, y, res;

    cin>>T;
    while(T--)
    {
        memset(shap,0,sizeof(shap));
        cin>>N;
        for(int i=0; i<N; i++)
        {
            cin>>x>>y;
            if(x<y)
            {
                m[i].from=x;
                m[i].to=y;
            }
            else
            {
                m[i].from=y;
                m[i].to=x;
            }
        }

        move tmp;
        int flag=0;
        for(int i=0; i<N; i++)
        {
            flag=0;
            for(int j=0; j<N-i-1; j++)
            {
                if(m[j].from>m[j+1].from)
                {
                    flag=1;
                    tmp = m[j];
                    m[j]=m[j+1];
                    m[j+1]=tmp;
                }
            }
            if(flag==0)
                break;
        }

        res=0;
        for(int i=0; i<N; i++)
        {
            for(int j=i+1;j<N;j++){
                if(i!=j && shap[j]==0)
                {
                    if(m[i].to>m[j].from||m[i].to==m[j].from){
                        shap[j]=1;
                        res++;
                    }
                }
            }
        }

        cout<<(res+1)*10<<endl;
    }
    return 0;
}

转:

View Code
 1 /*
 2 * PKU 1083 , Moving Tables
 3 * Created by PKKJ , 2008-05-17
 4 * 贪心
 5 */
 6 #include <stdio.h>
 7 #include <stdlib.h>
 8 
 9 /*
10 * 结构体work代表了一项搬桌子的工作,a与b分别代表的是起始和结束的位置,used则注明这项工作是否被安排
11 */
12 typedef struct _work
13 {
14     int a;
15     int b;
16     int used;
17 } work;
18 
19 int compare(const void* a, const void *b)
20 {
21     if ((*(work *)a).a == (*(work *)b).a)
22         return (*(work *)a).b > (*(work *)b).b ? 1 : -1;
23     return (*(work *)a).a > (*(work *)b).a ? 1 : -1;
24 }
25 
26 int main()
27 {
28     int num;
29     work queue[210];
30     scanf("%d", &num);
31     while (num--)
32     {
33         int i, j, n, u, time = 0;
34         scanf("%d", &n);
35         for (i = 0; i < n; i++)
36         {
37             int t;
38             scanf("%d%d", &queue[i].a, &queue[i].b);
39             /* 读入数据时注意顺序,a需要小于b */
40             if (queue[i].a > queue[i].b)
41             {
42                 t = queue[i].a;
43                 queue[i].a = queue[i].b;
44                 queue[i].b = t;
45             }
46             queue[i].used = 0;
47         }
48         /* 使用系统的qsort进行快速排序 */
49         qsort(queue, n, sizeof(queue[0]), compare);
50         /* 逐项搬桌子任务检查,看看是否完成了*/
51         for (i = 0; i < n; i++)
52         {
53             if (!queue[i].used)
54             {
55                 queue[i].used = 1;
56                 u = i;
57                 /*向后找相容的搬桌子任务:注意题目所给的条件,怎样是冲突,怎样是相容*/
58                 for (j = i + 1; j < n; j ++)
59                 {
60                     if (!queue[j].used)
61                     {
62                         if ((queue[j].a > queue[u].b + 1) || (queue[j].a - 1
63                                 == queue[u].b) && (queue[u].b % 2 == 0))
64                         {
65                             queue[j].used = 1;
66                             u = j;
67                         }
68                     }
69                 }
70                 time++;
71             }
72         }
73         printf("%d\n", time * 10);
74     }
75     return 0;
76 }

 

Description

The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. 

The floor has 200 rooms each on the north 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 are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving. 

For each room, at most one table will be either 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.

Input

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

Output

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

Sample Input

3 
4 
10 20 
30 40 
50 60 
70 80 
2 
1 3 
2 200 
3 
10 100 
20 80 
30 50 
原文地址:https://www.cnblogs.com/panderen/p/2439128.html