AtCoder Beginner Contest 131

はい. AtCoder Beginner Contest 131 です

久しぶりに参加したので,ちゃんと解けるか不安だったけど, A,B,C,D は解けました.
E も解けそうだったけど,時間が足りませんでした( C に時間をかけ過ぎた).

f:id:zeronosu77108:20190623155652p:plain f:id:zeronosu77108:20190623155657p:plain


A - Security

問題文

char で取って1つ前を保存しておきました.

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>


using namespace std;

int main(void) {
    char c1,c2;
    c2 = '-';
    bool f = false;
    for(int i=0; i<4; i++) {
        cin >> c1;

        if ( f || c1 == c2 ) {
            f = true;
        }
        c2 = c1;
    }

    cout << (f? "Bad" : "Good") << endl;
}



B - Bite Eating

問題文

愚直に計算しつつ,絶対値の小さいものを保存しておきました.

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>


using namespace std;

int main(void) {
    int n,l;
    int eat = 999;
    int sum = 0;
    cin >> n >> l;

    for(int i=1; i<=n; i++) {
        sum += l + i - 1;
        if( abs(eat) > abs(l+i-1) ) {
            eat = l+i-1;
        }
    }

    cout << sum - eat << endl;
}



C - Anti-Division

問題文

a未満の c,dで割りきれる数と b以下の割り切れる数を求めて引き算したら解けました.
最初 (b-a) で range を取ってごにょごにょしていたんですが,それではダメでした
(本当にこの方法で良いのか疑問を持ちながら書いていたので,まぁ当たり前と言えば当たり前でした)

この手の問題は,問題集とかにも載っているらしく,時間かかってしまったのは,ちょっと残念ですね.

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <cmath>


using namespace std;

long long gcd(long long a,long long b){
    if (a%b==0){
        return(b);
    }
    else{
        return(gcd(b,a%b));
    }
}

int main(void) {
    long long a,b,c,d;
    cin >> a >> b >>  c >> d;
    long long ans1,ans2;
    long long GCD = gcd(c,d);
    a--;
    ans1 = a/c + a/d - a/((c*d)/GCD);
    ans2 = b/c + b/d - b/((c*d)/GCD);

    cout << (b-a) - (ans2 - ans1) << endl;
}



D - Megalomania

問題文

期限の短い順に並べて,処理していけば 時間内に仕事が終わるかどうか分かります.
C より簡単なのでは…?

なんか compare の関数内側に書く方が良さそうなんだけど,書き方を覚えていなかったので外側に書くことになってしまった.
pair をほとんど調べずに進められたので,成長を感じました.

最初,期限の長いものから引き算していったので,変数名とかがそこに引きずられて残念な感じになっています…

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <cmath>
#include <utility>


using namespace std;


bool compare(pair<long long, long long> i1, pair<long long, long long> i2) 
{ 
    if ( i1.second == i2.second) return i1.first > i2.first;
    return (i1.second < i2.second); 
} 

int main(void) {
    long long n;
    cin >> n;
    vector< pair<long long, long long> > ab;
    for (long long i=0; i<n; i++) {
        long long tmp1,tmp2;
        cin >> tmp1 >> tmp2;
        ab.push_back( make_pair(tmp1,tmp2));
    }

    sort(ab.begin(), ab.end(), compare);
    long long max = 0;

    for (long long i=0; i<n; i++) {
        if ( max+ab[i].first > ab[i].second ) {
            cout << "No" << endl;
            return 0;
        }
        max += ab[i].first;
    }

    cout << "Yes" << endl;



}



E - Friendships

問題文

これもそんなに難しく無さそう…(希望的観測)
もう少し時間があったら解けた気もしますが,とりあえず解けませんでした.

勉強して書けたら追記します.



まとめ

今回は A,B,C,D 解けて,E もある程度解けそうだったので悪くないかもと思ったのですが,順位的には微妙でした.
次回はもっとレートを上げられるように頑張ります!