A. Vasya and Book

题解:分三种情况,看哪一种最优即可。是直接翻还是翻到最左再到Educational Codeforces Round 55 (Rated for Div. 2) A B C_i++,还是翻到最右再到Educational Codeforces Round 55 (Rated for Div. 2) A B C_i++

代码

#include<bits/stdc++.h>

using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int T,n,x,y,d;
cin>>T;
while(T--) {
cin >> n >> x >> y >> d;
int t = abs(y - x), cnt = 1e9;
if(t % d == 0) {
cnt = t / d;
}
if((y - 1) % d == 0){
cnt = min(cnt, (int)ceil(1.0 * x / d) + (y - 1) / d);
}
if((n - y) % d == 0) {
cnt = min(cnt, (int)ceil((n - x) * 1.0 / d) + (n - y) / d);
}
if(cnt != 1e9)
cout << cnt << endl;
else
puts("-1");
}
return 0;
}

B. Vova and Trophies

题解:维护两段和最大的连续子序列即可,并且保证最大间隔不会超过Educational Codeforces Round 55 (Rated for Div. 2) A B C_codeforces_03

代码

#include <bits/stdc++.h>

using namespace std;

int main() {
string str;
int n;
cin >> n;
cin >> str;
int res = 0;
int cntG = 0, sumG = 0, ccG = 0;
for (int i = 0; i < n; ++i) {
if (str[i] == 'G') {
sumG++;
cntG++;
} else {
ccG = cntG;
cntG = 0;
}
res = max(res, ccG + cntG + 1);
}
res = min(sumG, res);
cout << res << endl;
return 0;
}

C. Multi-Subject Competition

题解:根据能力值降序排序,考虑所选科目只有Educational Codeforces Round 55 (Rated for Div. 2) A B C_#include_04个人的最大能力值,然后前缀和,暴力枚举。

#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
vector <LL> v[100100];
LL a[100100];
int main() {
LL n, m, x, y, i;
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> x >> y;
v[x].push_back(y);
}
for (i = 1; i <= m; i++) {
if (v[i].size() > 0) {
sort(v[i].rbegin(), v[i].rend());
for (LL j = 1; j < v[i].size(); j++) {
v[i][j] = v[i][j] + v[i][j - 1];
}
}
}
for (LL i = 1; i <= m; i++) {
for (LL j = 0; j < v[i].size(); j++) {
if (v[i][j] > 0)
a[j] += v[i][j];
}
}
LL Answer = 0;
for (LL i = 0; i <= n; i++)
Answer = max(Answer, a[i]);
cout << Answer;
return 0;
}