A - Odd Index Sum
for文の問題です。
i%2==0の時だけ処理をしましょう。
C++
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
int ans = 0;
rep(i, n){
if(i%2==0) ans+=a[i];
}
cout << ans << endl;
return 0;
}
B - Longest Rainy Streak
Rが何文字だけ連続しているか判定しましょう。
最大数が答えです。
C++
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }
int main() {
int n;
cin >> n;
string s;
cin >> s;
int ans = 0;
int cnt = 0;
rep(i, n){
if(s[i] == 'R'){
cnt++;
}else{
ans = max(ans, cnt);
cnt = 0;
}
}
if(cnt) ans = max(ans, cnt);
cout << ans << endl;
return 0;
}
C - Bento Packing
bit全探索の問題です。
C++
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }
int main() {
ll n, w;
cin >> n >> w;
vector<ll> a(n), b(n);
rep(i, n) cin >> a[i] >> b[i];
ll ans = 0;
for(int bit=0; bit < (1<<n); bit++){
ll total_a = 0;
ll total_b = 0;
for (int i = 0; i < n; i++) {
if (bit & (1<<i)) {
total_a += a[i];
total_b += b[i];
}
}
if(total_b <= w) ans = max(ans, total_a);
}
cout << ans << endl;
return 0;
}
D - Back and Forth Distance
累積に関する問題かセグメント木の問題です。
C++
#include <bits/stdc++.h>
#include <atcoder/segtree>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define repx(i,x,n) for(int i=x; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
using namespace std;
using namespace atcoder;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b){ if(a>b){ a=b; return 1; } return 0; }
pair<int, int> op(pair<int, int> a, pair<int, int> b) { return a.first < b.first ? a:b; }
pair<int, int> e() { return {1e9, -1}; }
int main() {
int n, q;
cin >> n >> q;
vector<pair<int, int>> a(n), b(n);
segtree<pair<int, int>, op, e> segl(a), segr(b);
rep(i, n-1){
int l;
cin >> l;
segl.set(i+1, {l, i+1});
}
rep(i, n-1){
int r;
cin >> r;
segr.set(i, {r, i});
}
rep(i, q){
int s, t;
cin >> s >> t;
s--; t--;
if(s < t){
pair<int, int> r = segr.get(s);
pair<int, int> l = segl.get(s);
pair<int, int> d = segr.prod(0, s);
int ans = min(r.first, l.first + d.first);
cout << ans << endl;
}else{
pair<int, int> l = segl.get(s);
pair<int, int> r = segr.get(s);
pair<int, int> d = segl.prod(min(n-1, s+1), n);
int ans = min(l.first, r.first + d.first);
cout << ans << endl;
}
}
return 0;
}