A - Traffic Light
O(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() {
char c;
cin >> c;
map<char, char> mp;
mp['B'] = 'Y';
mp['Y'] = 'R';
mp['R'] = 'B';
cout << mp[c] << endl;
return 0;
}
B - Standing Outliers
O(N^2)
二重ループで全探索しましょう。
各要素毎に、他の全ての要素と比較して∣Xi−Xj∣≥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 n, d;
cin >> n >> d;
vector<int> x(n);
rep(i, n) cin >> x[i];
vector<int> ans;
for(int i=0; i<n; i++){
bool ok = true;
for(int j=0; j<n; j++){
if(i == j) continue;
int diff = abs(x[i] - x[j]);
if(d > diff) ok = false;
}
if(ok) ans.push_back(i);
}
cout << ans.size() << endl;
if(ans.size() == 0) return 0;
rep(i, ans.size()){
cout << ans[i] + 1 << ' ';
}
cout << endl;
return 0;
}
C - Range Search Query
O(NM)
累積和の問題です。
計算量は、最大でも4×10^5*10です。
シミュレーションしましょう。
文字列sの各要素を探索始点として、文字列tと等しいか判定します。
等しいなら累積和を前の要素+1した値を加算、等しくないなら前の要素をそのまま次の要素へ代入します。
指定された範囲のlとrの値を累積和と組み合わせて、Tが部分文字列として含まれるかを判定します。
C++
if(ans[max(r-m+1, 0)] - ans[l-1] > 0)
この判定で1以上ならYesになります。
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 q;
cin >> q;
string s, t;
cin >> s >> t;
int n = s.size();
int m = t.size();
vector<int> ans(n+1);
rep(i, n){
bool ok = true;
rep(j, m){
if(n <= i+j){
ok = false;
break;
}
if(s[i+j] != t[j]){
ok = false;
break;
}
}
if(ok) ans[i+1] = ans[i] + 1;
else ans[i+1] = ans[i];
}
rep(i, q){
int l, r;
cin >> l >> r;
if(ans[max(r-m+1, 0)] - ans[l-1] > 0) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
D - Masking Tape
文字を変更する時に毎回全ての要素に対してタイルが置かれているか判定、文字を変更するとTLEになります。
タイルが置かれる時にだけ判定して、最新の文字を記録します。
最後にタイルが置かれていない要素を、最新の文字に変更することで計算量を少なくできます。
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, q;
cin >> n >> q;
string s(n, 'a');
vector<int> t(n, -1);
vector<bool> tiles(n, false);
char last_char = 'a';
int last_time = -1;
rep(i, q){
int type;
cin >> type;
if(type == 1){
int x;
cin >> x;
x--;
if(tiles[x]){
t[x] = i;
}else{
if(t[x] < last_time) s[x] = last_char;
}
tiles[x] = !tiles[x];
}
if(type == 2){
char c;
cin >> c;
last_time = i;
last_char = c;
}
}
rep(i, n){
if(!tiles[i]){
if(t[i] < last_time) s[i] = last_char;
}
}
cout << s << endl;
return 0;
}