# 第十三届蓝桥杯 Java B 组省赛 D 题—— 最少刷题数（AC）

61 篇文章 329 订阅

## 1.最少刷题数

5
12 10 15 20 6

0 3 0 0 7

### 6.数据范围

1 ≤ N ≤ 100000 , 0 ≤ A i ≤ 100000 1≤N≤100000,0≤Ai≤100000

## Ac_code

#### C++

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
#define pb(s) push_back(s);
#define SZ(s) ((int)s.size());
#define ms(s,x) memset(s, x, sizeof(s))
#define all(s) s.begin(),s.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 200010;

int n;
int a[N];
int cnt[N];
void solve()
{
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
cnt[a[i]]++;
}
for (int i = 1; i <= 100000; ++i) {
cnt[i] += cnt[i - 1];
}
for (int i = 0; i < n; ++i) {
if (cnt[100000] - cnt[a[i]] <= (a[i] == 0 ? 0 : cnt[a[i] - 1])) {
cout << 0 << " ";
continue;
}
int l = a[i] + 1, r = 100000;
while (l < r) {
int mid = l + r >> 1;
if (cnt[100000] - cnt[mid] <= cnt[mid - 1] - 1) r = mid;
else l = mid + 1;
}
cout << r - a[i] << " ";
}
}
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
while (t--)
{
solve();
}
return 0;
}



#### Java

import java.io.*;

public class Main{
static int N = 200010;
static int[] a = new int[N], cnt = new int[N];
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

public static void main(String[] args) throws IOException {
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(s[i]);
cnt[a[i]]++;
}
for (int i = 1; i <= 100000; ++i) {
cnt[i] += cnt[i - 1];
}
for (int i = 0; i < n; ++i) {
if (cnt[100000] - cnt[a[i]] <=(a[i] == 0 ? 0 : cnt[a[i] - 1])) {
out.print(0 + " ");
continue;
}
int l = a[i] + 1, r = 100000;
while (l < r) {
int mid = l + r >> 1;
if (cnt[100000] - cnt[mid] <= cnt[mid - 1] - 1) r = mid;
else l = mid + 1;
}
out.print((r - a[i]) + " ");
}
out.flush();
}
}


• 26
点赞
• 29
收藏
觉得还不错? 一键收藏
• 打赏
• 13
评论
10-18
11-24 376
01-27 2692
03-24 390
03-24 460
04-08
07-28
05-13 855
12-25 570
03-22 209
03-30 422
01-28 346

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

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