Loading

Codeforces Round #280 (Div. 2)A、B、C、D


题目传送门

A. Vanya and Cubes

题目分析:

水题,没啥说的,打表+二分

AC代码

#include <bits/stdc++.h>
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rT printf("\nTime used = %.3lf\n", (double)clock()/CLOCKS_PER_SEC)

using namespace std;

int n;
const int N = 1010;

long long a[N];

int main() {
    io;
    long long sum = 0;
    for(int i = 1; i < N; i++) {
        sum += i;
        a[i] = a[i - 1] + sum;
    }
    
    cin >> n;
    
    cout << upper_bound(a, a + N, n) - a - 1 << '\n';
    
    return 0;

B. Vanya and Lanterns

题目分析

题意

给你\(n\)个灯和一条长度为\(l\)的街道,问你为了让所有灯照亮\([0,l]\)的区间,灯的照明范围\(d\)最小是多少

这题贪心做,找包含两个端点的\(n+2\)个点中,相邻两个点的最大距离,除以\(2\)就是答案了

AC代码

#include <bits/stdc++.h>
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;

const int N = 2020;
int a[N];

int n, l;
double ans;
int main() {
    io;
    cin >> n >> l;
    
    for(int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    ans = max(a[0], l - a[n - 1]);
    
    for(int i = 1; i < n; i++) ans = max(ans, (double)(a[i] - a[i - 1]) / 2.0);
    
    printf("%.10lf\n", ans);
    
    return 0;
}

C. Vanya and Exams

题目分析

题意:一个人想让自己考试总成绩的平均分高于\(avg\),每科的分数都不能高于\(r\),告诉你每一科的分数\(a_i\)和要提交多少次\(b_i\)卷子能让该科加一分,问你最少需要提交几次

这是一道模拟+贪心的题目,从提交次数最少的卷子开始去提交,最后使其平均分超过\(avg\)

#include <bits/stdc++.h>
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rT printf("\nTime used = %.3lf\n", (double)clock()/CLOCKS_PER_SEC)

using namespace std;
using ll = long long;
using PII = pair<ll, ll>;

const int N = 1e5 + 100;
PII a[N];
ll n, r, avg;
ll cnt, sum;

int main() {
    io;
    cin >> n >> r >> avg;
    
    for(int i = 1; i <= n; i++) {
        ll j, k;
        cin >> j >> k;
        a[i] = make_pair(k, j);
        sum += j;
    }
    
    sort(a + 1, a + n + 1);
    
    for(int i = 1; i <= n && sum < avg * n; i++) {
        ll cost = min(r - a[i].second, avg * n - sum);
        sum += cost;
        cnt += a[i].first * cost;
        
        if(sum >= avg * n) goto end;       
    }
    
    end:
    cout << cnt << '\n';
    return 0;
}

D. Vanya and Computer Game

题目分析

题意

两个人打怪兽,第一个人每秒打出\(x\)次伤害,第二个人每秒\(y\)次,问你最后一击是谁打出来的

两个人都会在\(lcm(x,y)\)的时间点同时打出伤害,此时间点可算作一轮。之后计算多轮后怪物的血量\(a\),即\(a=a\mod{(x+y)/gcd(x,y)}\)。当\(a=0\),即怪物只剩两滴血,两人同时攻击将其正好击杀,或\(a=(x+y)/gcd(x,y)-1\),即怪物只剩下一滴血,两个人此时同时攻击。当\(a\neq 0\),通过二分答案,得到第几次杀死怪物,再判断次数能否被\(x\)\(y\)整除,分别对应击杀的人。

AC代码

#include <bits/stdc++.h>
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rT printf("\nTime used = %.3lf\n", (double)clock()/CLOCKS_PER_SEC)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) a * b / __gcd(a, b);
using namespace std;
using ll = long long;

int n;
ll x, y, a;

int main() {
    io;
    cin >> n >> x >> y;
    
    for(int i = 0; i < n; i++) {
        cin >> a;
        ll gcd = gcd(x, y), lcm = lcm(x, y);
        
        a %= (x + y) / gcd; // (x+y)/gcd是每一个公共周期里两个人一共造成的伤害
        if(!a || a == (x + y) / gcd(x, y) - 1) // 两个人同时给致命一击的时候
            puts("Both");
        else {
            ll l = 1, r = lcm;
            while(l < r) {
                ll mid = l + r >> 1;
                if(mid / x + mid / y >= a) r = mid;
                else l = mid + 1;
            }
            if(l % y == 0) puts("Vanya");
            else puts("Vova");
        }
    }
   
    return 0;
}
posted @ 2021-03-30 10:26  Frank_Ou  阅读(70)  评论(0编辑  收藏  举报