A - First Contest of the Year
fがdを越えるまで7を加算する。
fをdで減算する。
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 d, f;
cin >> d >> f;
while(f <= d) f += 7;
cout << f - d << endl;
return 0;
}
B - Substring 2
2重ループで全探索できます。
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, m;
cin >> n >> m;
string s, t;
cin >> s >> t;
int ans = 1e9;
for(int i=0; i<n-m+1; i++){
int cnt = 0;
for(int j=0; j<m; j++){
int st = s[i+j] - t[j];
if(st<0) st+=10;
cnt += st;
}
ans = min(ans, cnt);
}
cout << ans << endl;
return 0;
}
C - 1D puyopuyo
スタックに近い問題です。
スタックはインデックスを指定できないからvectorを使います。
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> vt;
for(int i=0; i<n; i++){
int a;
cin >> a;
vt.push_back(a);
if(vt.size() >= 4){
int m = vt.size() - 1;
if(vt[m] == vt[m-1] && vt[m-1] == vt[m-2] && vt[m-2] == vt[m-3]){
rep(j, 4) vt.pop_back();
}
}
}
cout << vt.size() << endl;
return 0;
}
D - Tail of Snake
動的計画法の問題です。
漸化式はコードを解析してください。
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;
cin >> n;
vector<ll> a(n), b(n), c(n);
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
rep(i, n) cin >> c[i];
ll INF = 2e18;
vector<vector<ll>> dp(4, vector<ll>(n+1, 0));
for(int i=0; i<n; i++){
for(int j=0; j<=1; j++) chmax(dp[1][i+1], dp[j][i] + a[i]);
for(int j=1; j<=2; j++) chmax(dp[2][i+1], dp[j][i] + b[i]);
for(int j=2; j<=3; j++) chmax(dp[3][i+1], dp[j][i] + c[i]);
}
cout << dp[3][n] << endl;
return 0;
}