0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtCoder Beginner Contest 462

0
Posted at

A - Secret Numbers

問題文

forとcharをint型で判定する問題。

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() {
    string s;
    cin >> s;

    string ans;
    for(auto c:s){
        if(47 < (int)c && (int)c < 58) ans += c;
    }
    cout << ans << endl;

    return 0;
} 

B - Gift

問題文

二次元配列の問題です。

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<vector<int>> ans(n, vector<int>());
    rep(i, n){
        int k;
        cin >> k;
        rep(j, k){
            int a;
            cin >> a;
            ans[a-1].push_back(i+1);
        }
    }

    rep(i, n){
        cout << ans[i].size() << ' ';
        for(auto a:ans[i]){
            cout << a << ' ';
        }
        cout << endl;
    }

    return 0;
} 

C - Not Covered Points

問題文

データ構造の問題です。
xを基準に昇順でソートします。
順番にforで処理をしていきます。
yが最小の値なら「点1から点NまでのN個の点をどれも含まないようなiの個数」としてカウントします。
コンテストではmapと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;
    cin >> n;

    map<int, set<int>> mp;
    rep(i, n){
        int x, y;
        cin >> x >> y;
        mp[x].insert(y);
    }
    int ans = 0;
    set<int> st;
    for(auto& m:mp){
        int y = *m.second.begin();
        auto it = st.lower_bound(y);
        if(it == st.begin()) ans++;
        st.insert(m.second.begin(), m.second.end());
    }
    cout << ans << endl;

    return 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<pair<int, int>> vec;
    rep(i, n){
        int x, y;
        cin >> x >> y;
        vec.emplace_back(x,y);
    }
    sort(vec.begin(), vec.end());

    int ans = 0;
    int val = n;
    for(auto [x, y]:vec){
        val = min(val, y);
        if(val == y) ans++;
    }
    cout << ans << endl;

    return 0;
} 

D - Accomplice

問題文

イベントソートの問題です。

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() {
    ll n, d;
    cin >> n >> d;
    vector<tuple<ll, ll>> event;
    rep(i, n){
        ll s, t;
        cin >> s >> t;
        if(t - s >= d){
            event.emplace_back(s, 1);
            event.emplace_back(t-d+1, -1);
        }
    }
    sort(event.begin(), event.end());

    ll nn = event.size();
    ll time = 0;
    ll ans = 0;
    ll cnt = 0;
    rep(i, nn){
        auto [t, add] = event[i];
        ans += cnt * (cnt - 1) / 2 * (t - time);
        cnt += add;
        time = t;
    }
    cout << ans << endl;

    return 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?