现你准备搬入这条街,你能搬入一幢房子的条件是这幢房子没有人住在里面,与此同时由于你非常热爱与邻居进行交流,故而你需要你所入住的房子两边上都有住户。
现要你求最小的可能符合要求的房子数,以及最大的可能符合要求的房子数。
最大的情况为(#-#-#-),此种情况有二个位置满足条件,为最大,故输出2
输入的一行为测试用例数t(1 <= t <= 200000),
接下来t行,每行含两个整数n和k,(1 <= n <= 1,000,000,000,0 <= k <= n)
对于每个用例输出最小的可能数以及最大的可能数
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
#include <bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { long long n,k; cin>>n>>k; if(k>n-k) { if(n-k>=0) cout<<0<<" "<<n-k<<endl; else cout<<0<<" "<<0<<endl; } else { if(k-1>=0) cout<<0<<" "<<k-1<<endl; else cout<<0<<" "<<0<<endl; } } return 0; }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); while(t-- > 0){ String[] temp = br.readLine().trim().split(" "); long n = Long.parseLong(temp[0]), k = Long.parseLong(temp[1]); // 最小肯定是0 long min = 0; // 最大不会超过k-1,但是又不能比剩余能入住的房子n-k多 long max = 0; max = Math.min(n - k, Math.max(k - 1, 0)); System.out.println(min + " " + max); } } }
我的
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); } } } }
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)); } } } } }
#include<bits/stdc++.h> using namespace std; int main() { int n;cin>>n; while(n--) { int t,k;cin>>t>>k; cout<<0<<" "<<max(min(k-1,t-k),0)<<endl; } }
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)); } } } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { int n,k; cin>>n>>k; if(n<3||k==0||k>n){cout<<0<<" "<<0<<endl;} if(n>=3&&k>0){cout<<0<<" "<<min(n-k,k-1)<<endl;} } return 0; }
#include <iostream> #include <vector> using namespace std; int main(){ int numCyc; cin >> numCyc; while(numCyc--){ //最差怎么都是0 cout << 0 << ' '; int n, k; cin >> n >> k; //无论是总户数还是已住户数,只要小于2,就不行 if (n <= 2 || k <= 2) cout << 0 << endl; //如果已住小于总数的一半 else if (k <= (n+1) / 2) cout << k-1 << endl; //如果已住大于总数的一半 else{ //总户数为奇数的情况 if (n % 2 == 0) cout << n-k << endl; //总户数为偶数的情况 else cout << n/2 + (n+1)/2 - k << endl; } } return 0; }
t = int(input()) for i in range(t): n, k = map(int, input().split()) if n <= 2&nbs***bsp;n - k == 0&nbs***bsp;k == 1: print('0 0') else: s = '0 ' a = min(n-k, k-1) s = s + str(a) print(s)
#include<iostream> (720)#include<cstring> #include<cmath> using namespace std; int main() { int t; cin>>t; int A[t][3]; memset(A,0,sizeof(A)); for(int i=0;i<t;i++) { cin>>A[i][0]>>A[i][1]; int cmp = A[i][1]; if(cmp-1 < 0) { A[i][2] = 0; }else { int cmp1 = A[i][0] - A[i][1]; A[i][2] = min(cmp1,cmp-1);//在空位和夹空中,取最小值 } } for(int i=0;i<t;i++) { cout<<0<< " " <<A[i][2]<<endl; } return 0; }新手,
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { long n = sc.nextInt(); long k = sc.nextInt(); if (n <= 2 || k <= 1 || n == k) { System.out.println(0 + " " + 0); continue; } else { long gap = k - 1; long selections = n - k; long max = Math.min(gap, selections); System.out.println(0 + " " + max); } } } }
过80% 超时的Java老哥有吗 import java.util.Scanner; public class Main15 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); for (int t = 0; t < T; t++) { int n = scanner.nextInt(); int k = scanner.nextInt(); System.out.print( 0 + " "); if(n >= 2*k){ System.out.println(Math.max(k - 1, 0)); } else System.out.println(n-k); } }