A - mnclr
O(n)
配列の間に'o'を追加しましょう。
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() {
string s;
cin >> s;
int n = s.size();
rep(i, n){
cout << s[i];
if(i < n - 1){
cout << 'o';
}
}
cout << endl;
return 0;
}
B - Change
シミュレーションの問題です。
1.最小のお札の枚数を求める。
2.お釣りの金額を求める。
3.お釣りから一桁ずつ硬貨の枚数を求める
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];
vector<int> ans(3, 0);
rep(i, n){
int money = 0;
while(money < a[i]) money += 1000;
money -= a[i];
rep(j, 3){
ans[j] += money % 10;
money /= 10;
}
}
cout << ans[0] << ' ' << ans[1] << ' ' << ans[2] << endl;
return 0;
}
C - Walk the Line
累積和の問題です。
下記のループを考察で求めるのが難しいです。
C++
for(int i=0; i<=s; i++){
for(int j=s; j<n; j++){
ll x = p[s] - p[i];
ll y = p[j] - p[s];
if(min(2LL * x + y, x + 2LL * y) <= l){
ans = max(ans, j - i + 1);
}
}
}
sを起点に左側をp[s] - p[i]、右側をp[j] - p[s]で移動距離を求めます。
if(min(2LL * x + y, x + 2LL * y) <= l)にて移動距離が条件を満たしているか判定します。
条件を満たしているならans = max(ans, j - i + 1)にて街の数を求めます。
最大の街の数が答えです。
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, s, l;
cin >> n >> s >> l;
s--;
vector<ll> a(n-1), p(n);
rep(i, n-1) cin >> a[i];
rep(i, n-1) p[i+1] += p[i] + a[i];
int ans = 1;
for(int i=0; i<=s; i++){
for(int j=s; j<n; j++){
ll x = p[s] - p[i];
ll y = p[j] - p[s];
if(min(2LL * x + y, x + 2LL * y) <= l){
ans = max(ans, j - i + 1);
}
}
}
cout << ans << endl;
return 0;
}
C++