A - ADjacent
Aを固定して上下左右を判定します。
重実装の問題でした。
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 dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int main() {
int n = 2;
vector<string> s(n);
rep(i, n) cin >> s[i];
bool ok = false;
rep(i, n) rep(j, n){
if(s[i][j] == 'A'){
rep(k, 4){
int xx = dx[k] + j;
int yy = dy[k] + i;
if(xx < 0 || n <= xx || yy < 0 || n <= yy) continue;
if(s[yy][xx] == 'D') ok = true;
}
}
}
if(ok) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
B - Invalid Vote
最初に完全一致を探索、その後に一文字違いを探索しました。
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;
vector<string> s(n), t(m);
rep(i, n) cin >> s[i];
rep(i, m) cin >> t[i];
vector<int> ans(n);
rep(i, n){
int mm = t.size();
for(int j=mm-1; j>=0; j--){
if(s[i] == t[j]){
ans[i]++;
t.erase((t.begin() + j));
}
}
}
int mm = t.size();
rep(i, mm){
int success = 0;
rep(j, n){
int ni = t[i].size();
int nj = s[j].size();
if(ni != nj) continue;
int cnt = 0;
rep(k, ni){
if(t[i][k] == s[j][k]) cnt++;
}
if(ni - cnt <= 1) success++;
}
if(success == 1){
rep(j, n){
int ni = t[i].size();
int nj = s[j].size();
if(ni != nj) continue;
int cnt = 0;
rep(k, ni){
if(t[i][k] == s[j][k]) cnt++;
}
if(ni - cnt <= 1) ans[j]++;
}
}
}
rep(i, n){
cout << ans[i] << endl;
}
return 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, m;
cin >> n >> m;
vector<string> s(n), t(m);
rep(i, n) cin >> s[i];
rep(i, m) cin >> t[i];
vector<int> ans(n);
rep(j, m){
int x = -1;
rep(i, n){
if(s[i] == t[j]){
x = i;
}
}
if(x != -1){
ans[x]++;
continue;
}
x = -1;
int xcnt = 0;
rep(i, n){
int ni = s[i].size();
int nj = t[j].size();
if(ni != nj) continue;
int cnt = 0;
rep(k, ni){
if(t[j][k] != s[i][k]) cnt++;
}
if(cnt == 1){
x = i;
xcnt++;
}
}
if(xcnt == 1){
ans[x]++;
}
}
rep(i, n){
cout << ans[i] << endl;
}
return 0;
}
C - Self-Stopping Shower
前回ボタンを押した時間+kを今回のボタンを押す時刻の差分を計算します。
(今回ボタンを押した時間 - (前回ボタンを押した時間+k) + k - 1) / k
整数の切り上げ処理をしてkで割った値の合計が答えになります。
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, k, t;
cin >> n >> k >> t;
vector<ll> a(n);
rep(i, n) cin >> a[i];
sort(a.begin(), a.end());
for(auto it = a.rbegin(); it != a.rend(); it++){
if(*it > t) a.pop_back();
}
a.push_back(t);
ll nn = a.size();
ll ans = 0;
ll r = 0;
rep(i, nn){
if(r < a[i]){
ans += (a[i]-r + k-1) / k;
}
r = a[i] + k;
}
cout << ans << endl;
return 0;
}