POJ-3069&&POJ-2456

题目1:POJ3069
Saruman's Army
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11389 Accepted: 5759

Description

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.

Source

Stanford Local 2006

个人理解:直线上有n个点,点i的位置是Xi。从这n个点中选择若干个,给它们加上标记。对每一个点,其距离为R以内的区域里必须有带标记的点(本身为带有标记的点,可以认为与其距离为0的地方有一个带有标记的点)。在满足这个条件的情况下,希望能为尽可能少的点添加标记。求最少要有多少个点被加上标记。

贪心。首先把输入的Xi进行升序排序,从最左侧的点0开始,向右找出最远的一个点j,使得点j加标记后能覆盖点0而点(j+1)加标记后不能覆盖点0,那么点j为第一个要加标记的点,然后向右找出点j不能覆盖的第一个点m,从点m开始,重复上述步骤即可。

代码AC情况:


代码C:

# include <stdio.h>
# define N 1001
void change(int *a,int *b);//交换函数
void Qsort(int A[],int left,int right);//快速排序 升序
int A[N],num,R,S,P,i,sum;  
int main()  
{   
    while(scanf("%d%d",&R,&num)&&R!=-1||num!=-1)  
    {  
        for(i=0;i<num;i++)  
            scanf("%d",&A[i]);  
        Qsort(A,0,num-1);  
        i=sum=0;  
        while(i<num)  
        {  
            int S=A[i++];//S表示没有被覆盖的最左边的点   
            while(i<num&&A[i]<=S+R)//一直向右前进直到距S的距离大于R的点   
                i++;  
            P=A[i-1];//被标记的点   
            while(i<num&&A[i]<=P+R)//一直向右前进直到距P的距离大于R的点   
                i++;  
            sum++;  
        }  
        printf("%d\n",sum);  
    }  
    return 0;  
}  
void change(int *A,int *b){//交换函数 交换A b的值
   int c=*A;
   *A=*b;
   *b=c;
}
void Qsort(int A[],int left,int right)//升序
{
    int i=left,j=right,temp=A[left];
    if(left>=right)  return;
    while(i!=j)
    {
        while(A[j]>=temp && i<j) j--;
        while(A[i]<=temp && i<j) i++;
        if(i<j)
            change(&A[i],&A[j]);
    }
    if(i!=left)
    change(&A[left],&A[i]);
    Qsort(A,left,i-1);
    Qsort(A,i+1,right);
}
题目2:POJ2456
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15938 Accepted: 7628

Description

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). 

His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?

Input

* Line 1: Two space-separated integers: N and C 

* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

* Line 1: One integer: the largest minimum distance

Sample Input

5 3
1
2
8
4
9

Sample Output

3

Hint

OUTPUT DETAILS: 

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3. 

Huge input data,scanf is recommended.

Source

参考挑战程序设计143页

代码AC情况:


代码C:

# include <stdio.h>
# include <string.h>
# define MAX 100001 
void Qsort(int A[],int L,int R);
void change(int *a,int *b);
int X[MAX],N,M;  
int C(int d)  
{  
    int cot=1,T=X[0],i;  
    for(i=1;i<N;i++)  
    {  
        if(X[i]-T>=d)//如果牛舍距离大于安排的距离  
        {  
            cot++;//安排完成的牛+1  
            T=X[i];//从刚安排好牛的牛舍开始算距离  
            if(cot>=M)//如果所有的牛都安排完成牛舍还有多的  
            return 1;  
        }     
    }  
    return 0;//如果牛舍安排完成但是牛没有安排完  
} 
int main()  
{  
  int i,L,mid,R; 
  //freopen("AAA.txt","r",stdin);
  while(scanf("%d%d",&N,&M)!=EOF){  
     for(i=0;i<N;i++)  
    scanf("%d",&X[i]);  
    Qsort(X,0,N-1);  
     L=0;R=999999;  
    while(R-L>1)  
    {  
        mid=(L+R)/2;  
        if(C(mid)) L=mid;
        else R=mid;
   }  
   printf("%d\n",L);  
  }  
  return 0;  
}  
void change(int *a,int *b)//交换函数 交换a b的值  
{  
    int c=*a;  
    *a=*b;  
    *b=c;  
}  
void Qsort(int A[],int L,int R)//快速排序 升序   
{  
    int i=L,j=R,T=A[L]; //T为基准数  
    if(L>R)  return;  
    while(i!=j) //当数组左右两边没相遇  
    {  
        while(A[j]>=T&&i<j)j--;  //从右向左找  改为while(A[j]<=T&&i<j)  
        while(A[i]<=T&&i<j)i++; //从左向右找   改为while(A[i]>=T&&i<j) 这两处 改完即为降序   
        if(i<j)change(&A[i],&A[j]); //交换两数  
    }  
if(L!=i)change(&A[L],&A[i]);       //基准数归位  
    Qsort(A,L,i-1);         //递归左  
    Qsort(A,i+1,R);         //递归右  
}  



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值