A - Double-Sided Printing
整数の切り上げの式です。
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;
cout << (n+1)/2 << endl;
return 0;
}
B - Flip Card Stack
シミュレーションしましょう。
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;
vector<pair<int, int>> v;
rep(i, n) v.emplace_back(i+1, 0);
vector<int> k(q);
rep(i, q) cin >> k[i];
rep(i, q){
rep(j, k[i]){
v[j].second ^= 1;
}
reverse(v.begin(), v.begin() + k[i]);
}
rep(i, n){
auto [a, b] = v[i];
if(b) a *= -1;
cout << a << endl;
}
return 0;
}
C - Second Choice Election
setと二分探索の問題です。
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<vector<int>> v(n);
vector<int> h(n);
rep(i, m){
int a, b;
cin >> a >> b;
a--; b--;
h[a]++;
v[a].push_back(b);
}
set<pair<int, int>> st;
rep(i, n) st.insert({h[i], -i});
rep(i, n){
int nn = v[i].size();
st.erase({h[i], -i});
rep(j, nn){
st.erase({h[v[i][j]], -v[i][j]});
h[v[i][j]]++;
st.insert({h[v[i][j]], -v[i][j]});
}
int ans = st.rbegin()->second * -1;
cout << ans + 1 << endl;
rep(j, nn){
st.erase({h[v[i][j]], -v[i][j]});
h[v[i][j]]--;
st.insert({h[v[i][j]], -v[i][j]});
}
st.insert({h[i], -i});
}
return 0;
}
C++