Loading

AtCoder Beginner Contest 093

A - abc of ABC

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
char a[5];
int main() {
    for (int i = 0; i < 3; i++) cin >> a[i];
    sort(a, a + 3);
    for (int i = 0; i < 3; i++) {
        if (a[i] != 'a' + i) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}

B - Small and Large Integers

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int q;
LL x, y;
int main() {
    cin >> q;
    while (q--) {
        cin >> x >> y;
        if(x>y)swap(x,y);
        if(x==y){
            cout << 2 * x - 2 << endl;
            continue;
        }
        else if(x==y+1){
            cout << 2 * x - 2 << endl;
            continue;
        }
        LL c = sqrt(x * y);
        if (c * c == x * y) c--;
        if(c*(c+1)<x*y){
            cout << 2 * c - 1 << endl;
        }
        else
            cout << 2 * c - 2 << endl;
    }
    return 0;
}

C - Same Integers

给出abc三个数,现在可以进行如下操作

一种是选择两个数,将他们都加1

一种 选择一个数,将其加2

问多少次操作后可以使得三个数相等

算出最大的数和两个数差的和,如果是偶数直接除以2

否则就加上3后除以2即可

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int a[3];
int main() {
    cin >> a[0] >> a[1] >> a[2];
    sort(a, a + 3);
    int res = a[2] - a[0] + a[2] - a[1];
    if (res & 1) res += 3;
    cout << res / 2 << endl;
    return 0;
}

D - Worst Case

给出a和b,需要找出乘积比a*b小的不同组合

首先假设a<=b

对于a==b的情况,那么从1到a-1都可以找到对应的值(例如a-1找b+1),对应的1到b-1也可以找到,所以是2*(a-1)

对于a+1==b的情况,那么也是同样可以找到2*(a-1)

对于一般的情况,假设\(c*c<a*b\),那么对于1到c-1,都可以找到对应的值,但是要删去a这一项,如果\(c*(c+1)<a*b\),那么c也可以找到对应的项,所以分别是\(2*c-2\)\(2*c-1\)

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
int q;
LL x, y;
int main() {
    cin >> q;
    while (q--) {
        cin >> x >> y;
        if(x>y)swap(x,y);
        if(x==y){
            cout << 2 * x - 2 << endl;
            continue;
        }
        else if(x==y+1){
            cout << 2 * x - 2 << endl;
            continue;
        }
        LL c = sqrt(x * y);
        if (c * c == x * y) c--;
        if(c*(c+1)<x*y){
            cout << 2 * c - 1 << endl;
        }
        else
            cout << 2 * c - 2 << endl;
    }
    return 0;
}
posted @ 2021-02-05 11:11  dyhaohaoxuexi  阅读(64)  评论(0编辑  收藏  举报