Hướng dẫn giải của Lối Thoát Bí Ẩn
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Tác giả:
Code mẫu C++ của ceaturs
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MOD 1000000007LL
void SOLVED(){
int n,h; cin>>n>>h;
vector<int> a,b;
for(int i=1; i<=n/2; i++){
int c,d; cin>>c>>d;
a.push_back(c);
b.push_back(h-d);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int ans=INT_MAX, cnt =1;
int as=a.size(), bs=b.size();
for(int i=1; i<=h; i++){
int cnta=as-(lower_bound(a.begin(), a.end(), i)-a.begin()) +1;
int cntb=bs-(lower_bound(b.begin(), b.end(), i)-b.begin()) +1;
int cur=cnta + (bs-cntb);
if(cur<ans){
ans=cur;
cnt=1;
}
else if(cur==ans) cnt++;
}
cout<<ans<<" "<<cnt<<endl;
}
signed main(){
fastIO
int TESTCASE=1;
//cin>>TESTCASE;
while(TESTCASE--) SOLVED();
return 0;
}
Code mẫu Java
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
int n = sc.nextInt();
int h = sc.nextInt();
int[] a = new int[n / 2];
int[] b = new int[n / 2];
for (int i = 0; i < n / 2; i++) {
int c = sc.nextInt();
int d = sc.nextInt();
a[i] = c;
b[i] = h - d;
}
Arrays.sort(a);
Arrays.sort(b);
int ans = Integer.MAX_VALUE;
int cnt = 1;
int as = a.length, bs = b.length;
for (int i = 1; i <= h; i++) {
int cnta = as - lowerBound(a, i) + 1;
int cntb = bs - lowerBound(b, i) + 1;
int cur = cnta + (bs - cntb);
if (cur < ans) {
ans = cur;
cnt = 1;
} else if (cur == ans) {
cnt++;
}
}
System.out.println(ans + " " + cnt);
}
// lower_bound cho mảng thường
private static int lowerBound(int[] arr, int val) {
int left = 0, right = arr.length;
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid] < val) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
//Bên dưới là bộ đọc nhanh, có thể sử dụng bộ đọc Scanner có sẳn của java cũng được
/*
* Don't see below
*/
static Reader sc = new Reader();
static StringBuilder sb = new StringBuilder();
static Random rd = new Random();
static class Reader {
private int BUFFER_SIZE = 1 << 16;
private byte[] buffer = new byte[BUFFER_SIZE];
private int bufferPointer = 0, bytesRead = 0;
private InputStream rd;
public Reader() {
this.rd = System.in;
}
public Reader(String nameFile) {
try {
this.rd = new FileInputStream(nameFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private byte read() {
if (bufferPointer == bytesRead) {
bufferPointer = 0;
try {
bytesRead = rd.read(buffer, bufferPointer, BUFFER_SIZE);
} catch (IOException e) {
e.printStackTrace();
}
if (bytesRead == -1) {
return -1;
}
}
return buffer[bufferPointer++];
}
public int nextInt() {
int number = 0;
int c = read();
while (c <= ' ') {
c = read();
}
boolean negative = (c == '-');
if (negative) {
c = read();
}
do {
number = number * 10 + (c - '0');
c = read();
} while (c >= '0' && c <= '9');
return negative ? -number : number;
}
public long nextLong() {
long number = 0L;
int c = read();
while (c <= ' ') {
c = read();
}
boolean negative = (c == '-');
if (negative) {
c = read();
}
do {
number = number * 10 + (c - '0');
c = read();
} while (c >= '0' && c <= '9');
return negative ? -number : number;
}
public String next() {
int c = read();
while (c <= ' ') {
c = read();
}
StringBuilder t = new StringBuilder();
do {
t.append((char) c);
c = read();
} while (c > ' ');
return t.toString();
}
public String nextLine() {
int c = read();
while (c == '\n' || c == '\r') {
c = read();
}
StringBuilder t = new StringBuilder();
while (c != '\n' && c != '\r' && c != -1) {
t.append((char) c);
c = read();
}
return t.toString();
}
public double nextDouble() {
return Double.parseDouble(next());
}
public char nextChar() {
int c = read();
while (c <= ' ') {
c = read();
}
return (char) c;
}
}
}
Bình luận