前三题都是水题,第三题居然没有发现循环那里整数溢出,甚至没做出来,不懂当时怎么想的,太脑残了
- A - Chips Moving
题意:给一串序列,每个点x可以免费的跳到x-2或x+2处,跳到x+1或x-1处则需要一个费用,求让所有数抵达同一个点需要的最少费用
思路:可以看成所有奇数或所有偶数最终都能免费到达一个点,奇数偶数之间移动则需要费用,所以答案就是奇数和偶数个数更小的那个
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3+5;
const int inf = 0x3f3f3f3f;
int n, a[maxn], res[maxn];
int main()
{
cin >> n;
int e = 0, o = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i]&1)
o++;
else
e++;
}
cout << min(o, e) << endl;
return 0;
}
- B - Bad Prices
题意:给一串序列,求出序列数中右边存在比它小的数的这样的数的个数
思路:用ST表判断是否是当前最小即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e6+5;
const int inf = 0x3f3f3f3f;
int n, a[maxn], T, st[maxn][30];
int query(int l, int r)
{
if (l > r) return 0;
int k = log(r-l+1)/log(2.0);
return min(st[l][k], st[r-(1<<k)+1][k]);
}
int main()
{
cin >> T;
for (int t = 0; t < T; t++) {
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
st[i][0] = a[i];
}
for (int i = n; i >= 1; i--)
for (int j = 1; i+(1<<j)-1 <= n; j++) {
st[i][j] = min(st[i][j-1], st[i+(1<<j-1)][j-1]);
}
for (int i = 1; i <= n; i++) {
if (query(i, n) != a[i]) {
ans++;
}
}
cout << ans << "\n";
}
return 0;
}
- C - Book Reading
题意:给书页数量n,阅读能力m,sum表示m的倍数中所有不大于n的数的个位数之和,比如n=15,m=5,则有5,10,15,sum=5+0+5=10。求出sum的值
思路:显然m的倍数的个位数中每到0就是一次循环,求出一次循环中的所有数,答案就出来了
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e6+5;
const int inf = 0x3f3f3f3f;
ll q, n, m;
int main()
{
cin >> q;
for (int t = 0; t < q; t++) {
cin >> n >> m;
if (n < m) {
cout << 0 << "\n";
continue;
}
vector<ll> res;
ll ans = 0, k, sum = 0;
for (ll i = m; i <= n; i+=m) {
k = i%10;
res.push_back(k);
sum += k;
if (k == 0)
break;
}
ll p = (n/m)%res.size();
ans += ((n/m)/res.size())*sum;
for (int i = 0; i < p; i++)
ans += res[i];
cout << ans << "\n";
}
return 0;
}