湖北中医药大学2019暑期练习赛(四)

A - Saruman’s Army POJ - 3069

Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each palantir has a maximum effective range of R units, and must be carried by some troop in the army (i.e., palantirs are not allowed to “free float” in mid-air). Help Saruman take control of Middle Earth by determining the minimum number of palantirs needed for Saruman to ensure that each of his minions is within R units of some palantir.

Input
The input test file will contain multiple cases. Each test case begins with a single line containing an integer R, the maximum effective range of all palantirs (where 0 ≤ R ≤ 1000), and an integer n, the number of troops in Saruman’s army (where 1 ≤ n ≤ 1000). The next line contains n integers, indicating the positions x1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1.

Output
For each test case, print a single integer indicating the minimum number of palantirs needed.

Sample Input
0 3
10 20 20
10 7
70 30 1 7 15 20 50
-1 -1
Sample Output
2
4
Hint
In the first test case, Saruman may place a palantir at positions 10 and 20. Here, note that a single palantir with range 0 can cover both of the troops at position 20.

In the second test case, Saruman can place palantirs at position 7 (covering troops at 1, 7, and 15), position 20 (covering positions 20 and 30), position 50, and position 70. Here, note that palantirs must be distributed among troops and are not allowed to “free float.” Thus, Saruman cannot place a palantir at position 60 to cover the troops at positions 50 and 70.

题目大意

在一条直线上,有n个点。从这n个点中选择若干个,给他们加上标记。对于每一个点,其距离为R以内的区域里必须有一个被标记的点。问至少要有多少点被加上标记。

解题思路

先进行从小到大的排序,从最左边的开始考虑。对于这个点,到距其R以内的区域必须要有带有标记的点。带有标记的点一定在其右侧(包含这个点本身)。给从最左边开始,距离为R以内的最远的点加上标记,尽可能的覆盖更靠右边的点。对于添加了标记的点右侧相距超过R的下一个点,采用同样的方法找到最右侧R距离以内最远的点添加标记。在所有点都被覆盖之前不断重复这一过程。

源代码

#include <algorithm>
#include <iostream>

using namespace std;
int main()
{
    int n,len,count;
    int a[1005];
    while(cin>>len>>n&&(len!=-1&&n!=-1))
    {
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        count=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i;j<n;j++)//找到中心点
            {
                if(a[i]+len<a[j])
                {
                    i=j-1;
                    break;
                }
            }
            if(a[i]+len>=a[n-1])//所有点都已经覆盖了
            {
                count++;
                break;
            }

            for(int j=i;j<n;j++)//找到第一个未覆盖的点,下一次循环从这里开始
            {
                if(a[i]+len<a[j])
                {
                    i=j-1;
                    break;
                }
            }
            count++;
        }
        cout<<count<<endl;

    }
    return 0;
}

B - Fence Repair POJ - 3253

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn’t own a saw with which to cut the wood, so he mosies over to Farmer Don’s Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn’t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input
Line 1: One integer N, the number of planks
Lines 2… N+1: Each line contains a single integer describing the length of a needed plank
Output
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts
Sample Input
3
8
5
8
Sample Output
34
Hint
He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

题目大意

有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度,给定各个要求的小木板的长度,及小木板的个数n,求最小费用

解题思路

利用哈夫曼的思想,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些和累加到总费用中即可。用优先队列处理。
priority_queue q;      //通过操作,按照元素从大到小的顺序出队
priority_queue<int,vector, greater > q;   //通过操作,按照元素从小到大的顺序出队

源代码

#include <cstdio>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
	int n;
	long long temp,a,b,mincost;
	while(~scanf("%d",&n))
	{
		priority_queue<int, vector<int>, greater<int> > Q;

		while(!Q.empty())
			Q.pop();

		for(int i = 1; i <= n; i++)
		{
			scanf("%I64d",&temp);
			Q.push(temp);
		}

		mincost = 0;

		while(Q.size() > 1)
		{
			a = Q.top();
			Q.pop();
			b = Q.top();
			Q.pop();
			Q.push(a+b);
			mincost +=a+b;
		}
		printf("%I64d\n",mincost);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值