概述
1.3.1
Problem 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. 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.
|
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 |
Sample Output
10 20 30 |
巧妙地题型。贪心算法,预处理打表,建一个0的表来表示两个间的走廊,每用一次加一,多一个则表示时间多一个十分钟。
#include<stdio.h>
#include<string.h>
int save[205];
int main()
{
int m;
scanf("%d", &m);
while (m--)
{
int n;
scanf("%d", &n);
memset(save, 0, sizeof(save));
int a, b;
int tem1, tem2;
int i;
while (n--)
{
scanf("%d %d", &tem1, &tem2);
if (tem1 < tem2) { a = tem1, b = tem2; }
else { a = tem2, b = tem1; }
a = (a + 1) / 2;
b = (b + 1) / 2;
if (a == b)save[a]++;
else for (i = a; i <= b; i++)
save[i]++;
}
int max = 0;
for (i = 1; i <= 200; i++)
if (max < save[i])max = save[i];
printf("%dn", max * 10);
}
return 0;
}
1.3.2
Problem Description
“今年暑假不AC?”
“是的。” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。 作 为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、 超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目) |
Input
输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据 Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。
|
Output
对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。
|
Sample Input
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 0 |
Sample Output
5 |
#include<stdio.h>
#include<string.h>
int save[100][2];
int k[100];
int main()
{
int n;
while (scanf("%d", &n) && n != 0)
{
int i;
for (i = 0; i < n; i++)
scanf("%d %d", &save[i][0], &save[i][1]);
int x, y, tem;
int sum = 0;
for (x = 0; x < n - 1; x++)
for (y = 0; y < n - x - 1; y++)
if (save[y][1] - save[y][0]>save[y + 1][1] - save[y + 1][0])
{
tem = save[y][0];
save[y][0] = save[y + 1][0];
save[y + 1][0] = tem;
tem = save[y][1];
save[y][1] = save[y + 1][1];
save[y + 1][1] = tem;
}
for (y = 0; y < n; y++)
{
int w = 0;
for (x = save[y][0]+1; x <= save[y][1]-1; x++)
{
if (k[x] != 0) { w = 1; break; }
}
if (w == 0)
{
sum++;
for (x = save[y][0]; x <= save[y][1]; x++)
{
k[x] = 1;
}
}
}
printf("%dn", sum);
}
return 0;
}
1.3.3
Problem Description
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. |
Input
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.
|
Output
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
|
Sample Input
3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18 0 |
Sample Output
200 0 0 |
贪心算法
1,如果田忌的最快马快于齐王的最快马,则两者比。
(因为若是田忌的别的马很可能就赢不了了,所以两者比)
2,如果田忌的最快马慢于齐王的最快马,则用田忌的最慢马和齐王的最快马比。
(由于所有的马都赢不了齐王的最快马,所以用损失最小的,拿最慢的和他比)
3,若相等,则比较田忌的最慢马和齐王的最慢马
3.1,若田忌最慢马快于齐王最慢马,两者比。
(田忌的最慢马既然能赢一个就赢呗,而且齐王的最慢马肯定也得有个和他比,所以选最小的比他快得。)
3.2,其他,则拿田忌的最慢马和齐王的最快马比。(有三种可能,齐王最快和田忌最慢一样速度+0以及齐王最快比田忌最慢快-1)(反正所有的马都比田忌的最慢马快了,所以这匹马必输,选贡献最大的,干掉齐王的最快马)
注意:用cont在比较时计数,因为一共就sum匹马,所以比cont次,更加方便且不容易出错!
#include<stdio.h>
int main()
{
int sum, i;
while (scanf("%d", &sum) && sum != 0)
{
int my[1005];
int you[1005];
for (i = 0; i < sum; i++)scanf("%d", &my[i]);
for (i = 0; i < sum; i++)scanf("%d", &you[i]);
int tem;
int x, y;
for (x = 0; x < sum - 1; x++)
for (y = 0; y < sum - x - 1; y++)
{
if (my[y + 1]>my[y])
{
tem = my[y + 1];
my[y + 1] = my[y];
my[y] = tem;
}
if (you[y + 1]>you[y])
{
tem = you[y + 1];
you[y + 1] = you[y];
you[y] = tem;
}
}
int ma = 0, mb = sum - 1, ya = 0, yb = sum - 1, res = 0;
int cont = 0;
while ((cont++) < sum)
{
if (my[ma] > you[ya]) { res++; ya++; ma++; }
else if (my[ma] < you[ya]) { res--; mb--; ya++; }
else
{
if (my[mb] > you[yb]) { res++; yb--; mb--; }
else
{
if (my[mb] < you[ya]) { res--; }
mb--; ya++;
}
}
}
printf("%dn", res * 200);
}
}
1.3.4
Problem Description
时维九月,序属三秋,辽军大举进攻MCA山,战场上两军正交锋.辽军统帅是名噪一时的耶律-James,而MCA方则是派出了传统武将中草药123.双方 经过协商,约定在十一月八日正午十分进行射箭对攻战.中草药123早早就开始准备,但是他是武将而不是铁匠,造弓箭的活就交给聪明能干的你了,现在告诉你 每种弓箭规格,即箭身的长度,以及每种规格弓箭所需要的数目,要求你把需要的弓箭都输出.
弓箭的基本样子为 ">+---+>",其中"+---+"为箭身,数据保证箭身长度 > 2 |
Input
首先输入一个t,表示有t组数据,跟着t行:
每行一个N (N < 50 ),接下去有N行,第i行两个整数Ai , Bi,分别代表需要箭身长度为Ai的弓箭Bi枝. (Ai < 30 , Bi < 10 ) 输入数据保证每一个Ai都是不同的. |
Output
按照箭身的长度从小到大的顺序依次输出所有需要的弓箭,"每一种"弓箭后输出一个空行.
|
Sample Input
1 4 3 4 4 5 5 6 6 7 |
Sample Output
>+-+> >+-+> >+-+> >+-+> >+--+> >+--+> >+--+> >+--+> >+--+> >+---+> >+---+> >+---+> >+---+> >+---+> >+---+> >+----+> >+----+> >+----+> >+----+> >+----+> >+----+> >+----+> |
这一题有一些表述不清,注意要在每一个大组内进行排序!
#include <stdio.h>
int save[60][2];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
int i;
for (i = 0; i < n; ++i)
{
scanf("%d %d", &save[i][0], &save[i][1]);
}
int tem, x, y;
for (x = 0; x < n - 1; x++)
for (y = 0; y < n - x - 1; y++)
if (save[y + 1][0] < save[y][0])
{
tem = save[y][0];
save[y][0] = save[y + 1][0];
save[y + 1][0] = tem;
tem = save[y][1];
save[y][1] = save[y + 1][1];
save[y + 1][1] = tem;
}
for (i = 0; i < n; i++)
{
char key[50];
key[0] = '>';
key[1] = '+';
int w;
for (w = 2; w < save[i][0]; w++)
key[w] = '-';
key[w] = '+';
key[w + 1] = '>';
key[w + 2] = '