首页 > 试题广场 >

买房

[编程题]买房
  • 热度指数:4015 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
在一条街上有n幢房子,标号从1到n,两个在标号上相差为1的房子视为相邻,这些房子中有k幢房子已有住户。
现你准备搬入这条街,你能搬入一幢房子的条件是这幢房子没有人住在里面,与此同时由于你非常热爱与邻居进行交流,故而你需要你所入住的房子两边上都有住户。
现要你求最小的可能符合要求的房子数,以及最大的可能符合要求的房子数。

Note: 就样例来说,#代表已有住户,-代表空位,这种情况(###---),没有满足条件的房子,为最小,故输出0
最大的情况为(#-#-#-),此种情况有二个位置满足条件,为最大,故输出2

输入描述:
输入的一行为测试用例数t(1 <= t <= 200000),

接下来t行,每行含两个整数n和k,(1 <= n <= 1,000,000,000,0 <= k <= n)


输出描述:
对于每个用例输出最小的可能数以及最大的可能数
示例1

输入

6
1 0
1 1
2 0
2 1
2 2
6 4

输出

0 0
0 0
0 0
0 0
0 0
0 2
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t-->0){
            int n = sc.nextInt();
            int k = sc.nextInt();
            if(k<2||k==n||n<3){
                System.out.println(0+" "+0);
            }else if(n-k<k){
                System.out.println(0+" "+(n-k));
            }else{
                System.out.println(0+" "+(k-1));
            }
        }
    }
}

为啥复杂度这么高
编辑于 2020-08-31 19:14:44 回复(0)

我的

import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        long min=0,max;
        for(int i=1;i<=a;i++){
            long n=sc.nextLong();
            long k=sc.nextLong();
            if(n<3||k<2){
                max=0;
            }else{
                if(n%2==0){//n为双数
                    if(k==n/2||k==n/2+1){
                        max=n/2-1;
                    }else if(k<n/2){
                        max=k-1;
                    }else{
                        max=n-k;
                    }
                }else{//n为单数
                    if(k==n/2+1){
                        max=n/2;
                    }else if(k<n/2+1){
                        max=k-1;
                    }else{
                        max=n-k;
                    }
                }
            }
            System.out.println(min+" "+max);
        }
    }
}

别人的浓缩版

import java.util.*;
public class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    int test = sc.nextInt();
    for(int i=0; i<test; i++){
     int n = sc.nextInt();
     int k = sc.nextInt();
      //住满了
      if(n == k || n<3 || k<2){
        System.out.println("0 0");
      }
      else{
        int result = (n/2 >= k)?(k-1):(n-k);
        System.out.println("0 "+result);
      }
    }
  }
}
发表于 2019-09-20 00:40:10 回复(0)

Java

注意k有可能为0。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int k = sc.nextInt();
            if (n < 3) {
                System.out.println(0 + " " + 0);
            } else {
                if (n - k < k) {
                    System.out.println(0 + " " + (n - k));
                } else {
                    System.out.println(0 + " " + (k - 1 > 0 ? k - 1 : 0));
                }
            }
        }
    }
}
发表于 2019-07-06 11:02:21 回复(0)