A - Maximal Value
for文の問題です。
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];
int ans = 0;
for(int i=1; i<n-1; i++){
if(a[i-1] < a[i] && a[i] > a[i+1]) ans++;
}
cout << ans << endl;
return 0;
}
B - Corridor Watch
O(M+M+M)でもO(MD)もいろんな時からがあります。
コンテスト中はO(M+M+M)で回答しました。
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 m, d;
cin >> m >> d;
string s;
cin >> s;
int dd = 0;
vector<int> cnt(m);
for(int i=0; i<m; i++){
if(s[i] == 'G') dd = d + 1;
if(dd){
cnt[i]++;
dd--;
}
}
for(int i=m-1; i>=0; i--){
if(s[i] == 'G') dd = d + 1;
if(dd){
cnt[i]++;
dd--;
}
}
int ans = 0;
for(int i=0; i<m; i++){
if(cnt[i] == 0) ans++;
}
cout << ans << endl;
return 0;
}
本来はO(MD)の二重ループの問題だと思います。
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 m, d;
cin >> m >> d;
string s;
cin >> s;
vector<bool> cnt(m);
for(int i=0; i<m; i++){
if(s[i] == '.') continue;
int l = max(i-d, 0);
int r = min(i+d, m-1);
for(int j=l; j<=r; j++){
cnt[j] = true;
}
}
int ans = 0;
for(int i=0; i<m; i++){
if(cnt[i] == false) ans++;
}
cout << ans << endl;
return 0;
}
C - Between P and Q
制約が「1≤N≤10」は順列全探索ですね。
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> p(n), q(n), r(n);
rep(i, n) cin >> p[i];
rep(i, n) cin >> q[i];
rep(i, n) r[i] = i+1;
int ans = 0;
do{
if(p < r && r < q) ans++;
}while(next_permutation(r.begin(), r.end()));
cout << ans << endl;
return 0;
}
C++