若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1215: WiFi

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,315

题目描述

One day, the residents of Main Street got together and decided that they would install wireless internet on their street, with coverage for every house. Now they need your help to decide where they should place the wireless access points. They would like to have as strong a signal as possible in every house, but they have only a limited budget for purchasing access points. They would like to place the available access points so that the maximum distance between any house and the access point closest to it is as small as possible.
Main Street is a perfectly straight road. The street number of each house is the number of metres from the end of the street to the house. For example, the house at address 123 Main Street is exactly 123 metres from the end of the street.

输入

The first line of input contains an integer specifying the number of test cases to follow. The first line of each test case contains two positive integers n, the number of access points that the residents can buy, and m, the number of houses on Main Street. The following m lines contain the house numbers of the houses on Main Street, one house number on each line. There will be no more than 100 000 houses on Main Street, and the house numbers will be no larger than one million.

输出

For each test case, output a line containing one number, the maximum distance between any house and the access point nearest to it. Round the number to the nearest tenth of a metre, and output it with exactly one digit after the decimal point.

样例输入

1
2 3
1
3
10

样例输出

1.0

参考代码

#include <stdio.h>
#include <string.h>
int coins[1100000];
unsigned dp[1100000];
int comp(const int* a, const int *b) 
{
    return (*a)-(*b);
}
main() 
{
    int nc;
    int i,j;
    int CASES;
    int price;
    scanf("%d", &CASES);
    while(CASES--) 
    {
        memset(dp,-1,sizeof(dp));
        dp[0]=0;
        scanf("%d", &price);
        scanf("%d", &nc);
        for (i=0;i<nc;i++) 
        {
            scanf("%d", coins+i);
        }
        for (i=0;i<nc;i++) 
        {
            for (j=price-1;j>=0;j--) 
            {
                if(dp[j]==-1) continue;
                if(dp[j+coins[i]] > dp[j]+1) dp[j+coins[i]] = dp[j]+1;
            }
        }
        for (i=price;dp[i]==-1;i++);
        printf("%d %dn", i, dp[i]);
    }
}

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

原站点 Skip Dreams孤独患者
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist