A - Doors in the Center
奇数の時、
s[n / 2] = '='
偶数の時、
s[n/2-1] = '=';
s[n/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;
cin >> n;
string s = string(n, '-');
if(n % 2 == 0){
s[n/2-1] = '=';
s[n/2] = '=';
}else{
s[n/2] = '=';
}
cout << s << endl;
return 0;
}
B - Full House 3
フルハウスを作ることができる条件を求める。
・同じカードa
が3枚以上あるか、同じカードb
が2枚以上あるか
・同じカードa
が3枚以上あるか、同じカードb
が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 = 7;
vector<int> a(n);
rep(i, n) cin >> a[i];
map<int, int> mp;
rep(i, n) mp[a[i]]++;
int ok2 = 0;
int ok3 = 0;
for(auto m:mp){
if(m.second >= 3){
ok3++;
}else if(m.second >= 2){
ok2++;
}
}
if((ok2 >= 1 && ok3 >= 1) || ok3 >= 2){
cout << "Yes" << endl;
}else{
cout << "No" << 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 = 7;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<int> ans(13, 0);
rep(i, n) ans[a[i]-1]++;
sort(ans.rbegin(), ans.rend());
if(ans[0] >= 3 && ans[1] >= 2){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
return 0;
}
C - Uniqueness
一意の数を持っている人の中で、
もっとも大きい数を持っている人の番号
を求めてください。
-
一意の数を持っている人
データ構造のmapで求めることができます。 -
もっとも大きい数を持っている人の番号
ループをしてもっとも大きい数を判定しましょう。
int ans = -1;
int hasNo = 0;
rep(i, n){
if(mp[a[i]] == 1 && hasNo < a[i]){
hasNo = a[i];
ans = 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() {
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
map<int, int> mp;
rep(i, n) mp[a[i]]++;
int ans = -1;
int hasNo = 0;
rep(i, n){
if(mp[a[i]] == 1 && hasNo < a[i]){
hasNo = a[i];
ans = i+1;
}
}
cout << ans << endl;
return 0;
}
D - Bonfire
まず最初に風が吹く度に1つずつ煙の要素を作成して移動させていきます。
一定以上の煙の要素を風が吹くたびに移動させるとTLEになります。
風が吹く度に逆方向へ焚き火を移動させましょう。
高橋君の位置は、焚き火があった箇所r, cを元にr+R, c+Cで判定することができます。
それをset、 またはmapで判断することができますね。
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, R, C;
string s;
cin >> n >> R >> C >> s;
int r = 0, c = 0;
set<P> st;
st.emplace(0, 0);
string ans;
rep(i, n){
if(s[i] == 'N') r++;
if(s[i] == 'S') r--;
if(s[i] == 'W') c++;
if(s[i] == 'E') c--;
st.emplace(r, c);
if(st.find({r+R, c+C}) != st.end()) ans += "1";
else ans += "0";
}
cout << ans << endl;
return 0;
}