# 牛客练习赛86A-C题解

N <= 100

## C

1 5 12 23......

6:2次
7:3次
8:4次
9:5次
10:2次
11:3次

13:2次
14:3次
15:4次
16:5次
17:2次
18:3次
19:4次
20:5次
21:6次
22:3次
...

a[i] + "第i - 1个峰值"恰好等于a[i + 1]，则要将整体减去一个a[i]（从贪心上讲这样最优）

m[1] = a[2] - 1;
for (int i = 2; i <= n - 1; i++) {
tmp[i] = (a[i + 1] - m[i - 1]) / a[i];
m[i] = tmp[i] * a[i] + m[i - 1];
if (m[i] == a[i + 1]) {
m[i] -= a[i];
tmp[i] -= 1;
}
}
ans[1] = a[2] - 1;
for (int i = 2; i <= n - 1; i++) {
ans[i] = tmp[i] + ans[i - 1];
}


pos = lower_bound(m + 1, m + n, b) - m;
if (m[pos] != b) pos--;
if (b <= a[pos + 1]) {
cout << m[pos] << " " << ans[pos] << "\n";
}
else {
ans1 = a[pos + 1] * ((b - m[pos]) / a[pos + 1]) + m[pos];
ans2 = ((b - m[pos]) / a[pos + 1]) + ans[pos];
cout << ans1 << " " << ans2 << "\n";
}


AC代码如下：

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[200010];
ll m[200010];
ll ans[200010];
ll tmp[200010];
ll n;
ll q;
ll b;
ll ans1;
ll ans2;
ll pos;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
m[1] = a[2] - 1;
for (int i = 2; i <= n - 1; i++) {
tmp[i] = (a[i + 1] - m[i - 1]) / a[i];
m[i] = tmp[i] * a[i] + m[i - 1];
if (m[i] == a[i + 1]) {
m[i] -= a[i];       //如果峰值数组恰好为a[i + 1],则减去一个a[i]
tmp[i] -= 1;        //倍数-1
}
}
ans[1] = a[2] - 1;
for (int i = 2; i <= n - 1; i++) {
ans[i] = tmp[i] + ans[i - 1];
}
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> b;
if (b < a[2]) {
cout << b << " " << b << "\n";
}
else {
pos = lower_bound(m + 1, m + n, b) - m;
if (m[pos] != b) pos--;
if (b <= a[pos + 1]) {
cout << m[pos] << " " << ans[pos] << "\n";
}
else {
ans1 = a[pos + 1] * ((b - m[pos]) / a[pos + 1]) + m[pos];
ans2 = ((b - m[pos]) / a[pos + 1]) + ans[pos];
cout << ans1 << " " << ans2 << "\n";
}
}
}
return 0;
}

posted @ 2021-07-10 11:22  Leins  阅读(93)  评论(0编辑  收藏  举报