A - chmin
for文、if文の問題です。
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, x;
cin >> n >> x;
vector<int> a(n);
rep(i, n) cin >> a[i];
rep(i, n){
if(a[i] < x){
x = a[i];
cout << 1 << endl;
}else{
cout << 0 << endl;
}
}
return 0;
}
B - Pepper Addiction
for文、if文の問題です。
今回はminでbとc[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 main() {
int n, m;
cin >> n >> m;
vector<int> c(m);
rep(i, m) cin >> c[i];
int ans = 0;
rep(i, n){
int a, b;
cin >> a >> b;
a--;
int k = min(b, c[a]);
ans += k;
c[a] -= k;
}
cout << ans << endl;
return 0;
}
C - Except and Min
貪欲法です。
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; }
void solve(vector<int> &a, map<int, int> &mp){
int k;
cin >> k;
vector<int> b(k);
rep(i, k){
cin >> b[i];
b[i]--;
mp[a[b[i]]]--;
}
for(auto m:mp){
if(m.second > 0){
cout << m.first << endl;
break;
}
}
rep(i, k){
mp[a[b[i]]]++;
}
}
int main() {
int n, q;
cin >> n >> q;
vector<int> a(n);
rep(i, n) cin >> a[i];
map<int, int> mp;
rep(i, n) mp[a[i]]++;
rep(i, q){
solve(a, mp);
}
return 0;
}
D - Integer-duplicated Path
深さ優先探索の問題です。
どうやってそのアルゴリズムを選択できるのかと言われると返答が難しいです。
おぼろげながら浮かんできたんです。深さ優先探索というアルゴリズムが
問題文の単純パスから、BFSではなくてDFSと考察できます。
探索していく中で頂点の数字とその数をmapで保持します。
同じ数字の頂点があったタイミングでフラグをtrueにします。
探索が終わるタイミングで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;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<vector<int>> graph(n, vector<int>());
rep(i, n-1){
int u, v;
cin >> u >> v;
u--; v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
map<int, int> mp;
vector<bool> used(n, false);
vector<bool> ans(n, false);
auto dfs = [&](auto dfs, int v, bool twoVertices){
if(used[v]) return;
used[v] = true;
mp[a[v]]++;
if(mp[a[v]] > 1) twoVertices = true;
ans[v] = twoVertices;
for(auto g:graph[v]){
dfs(dfs, g, twoVertices);
}
mp[a[v]]--;
used[v] = false;
};
dfs(dfs, 0, false);
rep(i, n){
if(ans[i]) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}