1
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 419

Posted at

A - AtCoder Language

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

    if(s == "red"){
        cout << "SSS" << endl;
    }else if(s == "blue"){
        cout << "FFF" << endl;
    }else if(s == "green"){
        cout << "MMM" << endl;
    }else{
        cout << "Unknown" << endl;
    }

    return 0;
} 

B - Get Min

問題文
データ構造の問題です。
mulchsetで解きましたが、正攻法はプライオリティキューです。

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 q;
    cin >> q;
    multiset<int> mst;
    rep(i, q){
        int t;
        cin >> t;
        if(t == 1){
            int x;
            cin >> x;
            mst.insert(x);
        }
        if(t == 2){
            cout << *mst.begin() << endl; 
            mst.erase(mst.begin());
        }
    }
    return 0;
} 

プライオリティキューver

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 q;
    cin >> q;
    priority_queue<int, vector<int>, greater<>> pq;
    rep(i, q){
        int t;
        cin >> t;
        if(t == 1){
            int x;
            cin >> x;
            pq.push(x);
        }
        if(t == 2){
            cout << pq.top() << endl; 
            pq.pop();
        }
    }
    return 0;
} 

C - King's Summit

問題文
数学的な問題です。
その場に留まるか、8近傍のマスに移動する。
とあり、その場に留まることができるので最小値と最大値にだけ注目します。

max((r[n-1] - r[0])/2, (c[n-1] - c[0])/2)

で良さそうです。
下記のサンプルで考察します。

3
2 1
2 1
3 1

だと0になります。
正数の割り算の切り上げ処理が必要になります。

max((r[n-1] - r[0] + 1)/2, (c[n-1] - c[0] + 1)/2)

考察が終わりました。

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> r(n), c(n);
    rep(i, n) cin >> r[i] >> c[i];
    sort(r.begin(), r.end());
    sort(c.begin(), c.end());
    cout << max((r[n-1] - r[0] + 1)/2, (c[n-1] - c[0] + 1)/2) << endl;
    return 0;
} 

D - Substr Swap

問題文
全探索で解くと文字列の結合の処理が重くTLEになります。
前処理で文字列を入れ替えるr, cを何かしらの処理をして、s, tを操作する必要があると気づくことができます。
n <= 5×10^5なので、文字列の配列を用意できます。

サンプル1で考察しましょう。

5 3
apple
lemon
2 4
1 5
5 5

rの入れ替える開始の要素に+1, cの入れ替える終了の要素に-1を加算します。

C++
    vector<int> rc(n+1);
    rep(i, m){
        int l, r;
        cin >> l >> r;
        l--;
        rc[l]++;
        rc[r]--;
    }
0 1 2 3 4 5
0 1 0 0 -1 0
1 1 0 0 -1 -1
1 1 0 0 0 -2

imos法で各文字が奇数、偶数回ほど入れ替わったか計算します。

C++
    rep(i, n){
        rc[i+1] = rc[i+1] + rc[i];
    }
0 1 2 3 4 5
1 2 2 2 2 0

1, 2, 3, 4の要素が偶数なのでsの文字
0の要素が奇数なので、tの文字
だと分かります。

lpple

合っていますね。
考察が終わりました。

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;
    string s, t;
    cin >> s >> t;
    vector<int> rc(n+1);
    rep(i, m){
        int l, r;
        cin >> l >> r;
        l--;
        rc[l]++;
        rc[r]--;
    }
    rep(i, n){
        rc[i+1] = rc[i+1] + rc[i];
    }
    rep(i, n){
        if(rc[i] % 2 == 0){
            cout << s[i];
        }else{
            cout << t[i];
        }
    }
    cout << endl;
    return 0;
} 
1
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
1
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?