A - Compromise
forで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;
cin >> n;
vector<int> x(n);
rep(i, n) cin >> x[i];
rep(i, n){
if(x[i] >= 0){
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
}
B - Representative Balls
データ構造の問題です。
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, m;
cin >> n >> m;
vector<int> c(n), s(n);
rep(i, n) cin >> c[i] >> s[i];
map<int, int> mp;
rep(i, n){
mp[c[i]] = max(mp[c[i]], s[i]);
}
rep(i, m){
if(mp.find(i+1) != mp.end()){
cout << mp[i+1] << ' ';
}else{
cout << -1 << ' ';
}
}
cout << endl;
return 0;
}
C - Count Close Pairs
しゃくとり法です。
このコードの書き方はテンプレートなので覚えてしまうのが早いですね。
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;
ll ans = 0;
ll right = 2;
for(ll left=1; left<=n; left++){
while(right <= n){
cout << "? " << left << ' ' << right << endl;
string ret;
cin >> ret;
if(ret == "No") break;
right++;
}
ans += right - left - 1;
if(right <= left + 1) right++;
}
cout << "! " << ans << endl;
return 0;
}
D - Placing Rooks
データ構造の問題です。
行を管理するデータ構造、列を管理するデータ構造を作り要素を追加、削除していきます。
D - Lampの類似問題。
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> r(m), c(m);
rep(i, m){
cin >> r[i] >> c[i];
r[i]--; c[i]--;
}
vector<set<int>> h(n), w(n);
rep(i, m){
for(auto it:h[r[i]]){
w[it].erase(r[i]);
}
h[r[i]].clear();
for(auto it:w[c[i]]){
h[it].erase(c[i]);
}
w[c[i]].clear();
h[r[i]].insert(c[i]);
w[c[i]].insert(r[i]);
}
ll ans = 0;
rep(i, n){
ans += h[i].size();
}
cout << ans << endl;
return 0;
}