实验5贪心算法 2
![实验5贪心算法 2](https://img.360docs.net/imgcd/06664mud1pn2kn7dieao-d1.webp)
![实验5贪心算法 2](https://img.360docs.net/imgcd/06664mud1pn2kn7dieao-a2.webp)
宁波工程学院电信学院计算机教研室
实验报告
课程名称:ACM
实验项目:贪心算法指导教师:
实验位置:姓名:学号:班级:日期:
一、实验目的
1.掌握贪心算法的基本要素。
2.了解并掌握数字事件序列问题、区间覆盖问题以及两头贪心问题的解题思路和算法。
3.学会贪心算法的算法步骤。
二、实验环境
具有Windows XP或2003的计算机、Vistual C++ 6.0、网络环境。
三、实验内容
[A] Moving Tables
问题描述
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.
输入
The 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 , 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 N+3-rd line, the remaining test cases are listed in the same manner as above.
输出
The output should contain the minimum time in minutes to complete the moving, one per line.
样例输入
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
源代码:
#include
#include
typedef struct _work
{
int a;
int b;
int used;
} work;
int compare(const void* a, const void *b)
{
if ((*(work *)a).a == (*(work *)b).a)
return (*(work *)a).b > (*(work *)b).b ? 1 : -1; return (*(work *)a).a > (*(work *)b).a ? 1 : -1;
}
int main()
{
int num;
work queue[210];
scanf("%d", &num);
while (num--)
{
int i, j, n, u, time = 0;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
int t;
scanf("%d%d", &queue[i].a, &queue[i].b);
if (queue[i].a > queue[i].b)
{
t = queue[i].a;
queue[i].a = queue[i].b;
queue[i].b = t;
}
queue[i].used = 0;
}
qsort(queue, n, sizeof(queue[0]), compare);
for (i = 0; i < n; i++)
{
if (!queue[i].used)
{
queue[i].used = 1;
u = i;
for (j = i + 1; j < n; j ++)
{
if (!queue[j].used)
{
if ((queue[j].a > queue[u].b + 1) || (queue[j].a - 1
== queue[u].b) && (queue[u].b % 2 == 0))
{
queue[j].used = 1;
u = j;
}
}
}
time++;
}
}
printf("%d\n", time * 10);
}
return 0;
}
[B] Tian Ji -- The Horse Racing
问题描述
Here is a famous story in Chinese history.
"That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others."
"Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in
a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser."
"Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian."
"Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match."
"It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"
Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum
weighted perfect matching...
However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses --- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.
In this problem, you are asked to write a program to solve this special case of matching problem.
输入
The input consists of up to 50 test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses. The input ends with a line that has a single 0 after the last test case.
输出
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
样例输入
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
样例输出
200
源代码:
#include
#include
int cmp(const void *a,const void *b) {
return *(int *)b - *(int *)a;
}
int main()
{
int i,j,k,s,x,n,t[1000],q[1000]; while (scanf("%d",&n),n)
{
for (i=0;i scanf("%d",&t[i]); for (i=0;i scanf("%d",&q[i]); qsort(t,n,sizeof(t[0]),cmp); qsort(q,n,sizeof(q[0]),cmp); for (i=0;i { if(t[0] >= q[i]) break; } for (s=-200*n;i { x=-i*200; for (j=i,k=0;j { if (t[k]>q[j]) x+=200; else if (t[k] x-=200; } if (x>s) s=x; } printf("%d\n",s); } return 0; } [C] 今年暑假不AC 问题描述 今年暑假不AC?” “是的。” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。 作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常 6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目) 输入 输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。 输出 对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。 样例输入 12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 样例输出 5 源代码: #include int main() { int i,j,k,n,a[101],b[101],l,m[101],p,o,t,d; while(scanf("%d",&n)!=EOF&&n!=0) { for(i=0;i { scanf("%d%d",&a[i],&b[i]); } for (i=1; i { for (j=0; j { if (b[j]>b[j+1]) { k=a[j];l=b[j]; a[j]=a[j+1];b[j]=b[j+1]; a[j+1]=k;b[j+1]=l; } } } int A[101]={0}; for(i=0;i { d=i-1; int p=0; while(d>=0) { if(a[i]>=b[d]) {