Problem Description
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.
3 4 10 20 30 40 50 60 70 80 2 1 3 2 200 3 10 100 20 80 30 50
10 20 30解题思路: 一看到题目,想到了贪心算法,只是在数据处理时注意了点,由于数据是上下的,所以要处理一下 从头到尾,找到可以一起搬得并标记已搬过,重复前面的动作即可。在杭电ACM课件里也介绍了也是贪心的方 法,他只是找出最大重复乘以10即可,这个证明我还不是能证出来,只是能体会到做法的合理性。以下是我的写的贪心法代码:
- #include<iostream>
- #include <algorithm>
- using namespace std;
- const int Max(210);
- struct node
- {
- int s;
- int e;
- }data[Max];
- int cmp(const void *a,const void *b)
- {
- return ((struct node*)a)->s-((struct node*)b)->s;
- }
- int main()
- {
- int n, test;
- cin>>test;
- while(test--)
- {
- cin>>n;
- for(int i = 1; i <= n; i ++)
- {
- cin>>data[i].s>>data[i].e;
- if(data[i].s>data[i].e)
- swap(data[i].s,data[i].e);
- }
- qsort(&data[1], n, sizeof(data[1]), cmp);
- int flag[Max];
- memset(flag,0,sizeof(flag));
- // for(i=1;i<=n;i++)
- // cout<<data[i].s<<'/t'<<data[i].e<<endl;
- int pre, sum = 0, step = n;
- while(step)
- {
- for(i = 1; i <= n; i ++)
- {
- if(! flag[i])
- {
- pre = i; break;
- }
- }
- flag[pre] = 1;
- step --;
- for(i=pre+1;i<=n;i++)
- {
- if(! flag[i])
- {
- if(data[i].s % 2 == 0 && data[i].s - 1 > data[pre].e)
- {
- flag[i] = 1; pre = i; step --;
- }
- if(data[i].s%2==1&&data[i].s>data[pre].e)
- {
- flag[i] = 1; pre = i; step --;
- }
- }
- }
- sum ++;
- }
- cout<<sum * 10<<endl;
- }
- return(0);
- }
以下是杭电ACM课件介绍的代码:
- #include <iostream>
- using namespace std;
- int main()
- {
- int t,i,j,N,P[200];
- int s,d,temp,k,min;
- cin>>t;
- for(i=0;i<t;i++)
- {
- for(j=0;j<200;j++)
- P[j]=0;
- cin>>N;
- for(j=0;j<N;j++)
- {
- cin>>s>>d;
- s=(s-1)/2;
- d=(d-1)/2;
- if(s>d)
- {
- temp=s;
- s=d;
- d=temp;
- }
- for(k=s;k<=d;k++)
- P[k]++;
- }
- min=-1;
- for(j=0;j<200;j++)
- if(P[j]>min)
- min=P[j];
- cout<<min*10<<endl;
- }
- return 0;
- }