Codeforces - 1088B & 1084B & 1084A & 1077C

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zxzxzx0119/article/details/84997608

Codeforces - 1088B & 1084B & 1084A & 1077C

  • Codeforces-1088B - Ehab and subtraction
  • Codeforces-1084B - Kvass and the Fair Nut
  • Codeforces-1084A - The Fair Nut and Elevator
  • Codeforces-1077C - Good Array

Codeforces-1088B - Ehab and subtraction

题目链接

题目大意

给你nk,以及n个数,要你从这n个数中选取k次,每次选最小的,并且选完之后所有的数要减去这个选出来的数。如果里面的数都是0了,就输出0
在这里插入图片描述

解析

先排序,然后模拟这个过程:

  • 每次找的时候要找第一个不是0的数,如果找到末尾,就输出0
  • 设置一个tmp变量记录当前数要减去的值,这个tmp的值是递增的;
  • tmp递增之后,每次向后减去和tmp相同的所有的值,最后一个>tmp的值也减去tmp(这个值就是下一次输出的值);

代码:

#include <bits/stdc++.h>
const int MAX = 100000 + 1;

int main(int argc, char const **argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, k, arr[MAX]; 
    std::cin >> n >> k;
    for(int i = 0; i < n; i++) 
        std::cin >> arr[i];    
    std::sort(arr, arr+n);
    int tmp = 0;
    int pos = 0;
    for(int i = 0; i < k; i++){ 
        while(arr[pos] == 0 && pos < n)
            pos++;
        if(pos == n){ 
            std::cout << 0 << std::endl;
            continue;
        }
        std::cout<< arr[pos] << std::endl;
        tmp += arr[pos];
        pos++;
        while(arr[pos] == tmp) // next equal elements should subtract tmp
            arr[pos++] -= tmp;
        arr[pos] -= tmp; // the last greater than tmp also subtract tmp
    }
    return 0;
}

或者稍微改进一下:

#include <bits/stdc++.h>

const int MAX = 100000 + 1;

int main(int argc, char const **argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, k, arr[MAX]; 
    std::cin >> n >> k;
    for(int i = 0; i < n; i++) 
        std::cin >> arr[i];    
    std::sort(arr, arr+n);
    int tmp = 0, pos = 0;
    for(int i = 0; i < k; i++){ 
        while(arr[pos] == tmp && pos < n)
            pos++;
        if(pos == n)
            std::cout << 0 << std::endl;
        else 
            std::cout << arr[pos]-tmp << std::endl;
        tmp = arr[pos];
    }
    return 0;
}

Codeforces-1084B - Kvass and the Fair Nut

题目链接

题目大意

就是给你n桶酒,还有一个杯子容量为s,以及n桶酒里面的酒的容量,要你用这n桶酒要装满这个容量为s的杯子,要你使得最后剩余的n个桶子中最小容量的桶子剩余的酒最多,求这个数。

在这里插入图片描述

解析

贪心,先减去所有不是最小的桶子的那些"和",看是否已经够了,如果不够,就每次将最小值减一,然后判断即可。

#include <bits/stdc++.h>

const int MAX = 1001;
typedef long long ll;

int main(int argc, char const** argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    ll n, s, arr[MAX];
    std::cin >> n >> s;
    ll sum = 0; 
    ll minn = 1000000000;
    for(int i = 0; i < n; i++){ 
        std::cin >> arr[i];
        sum += arr[i];
        if(arr[i] < minn)
            minn = arr[i];
    }
    if(sum < s) 
        std::cout << -1 << std::endl;
    else{
        sum -= minn*n; 

        if(sum >= s)
            std::cout << minn << std::endl;
        else { 
            for(ll surplus = s - sum; surplus > 0; minn--)
                surplus -=  n;
            std::cout << minn << std::endl;
        }
    }
    return 0;
}

Codeforces-1084A - The Fair Nut and Elevator

题目链接

题目大意

就是给你一个数n,代表n层楼,第i层楼有arr[i]个人,问你电梯最初停在哪一层,可以使得电梯走的层数最少。电梯走的方法如下:
在这里插入图片描述
在这里插入图片描述

解析

直接模拟即可。

#include <bits/stdc++.h>

const int MAX = 101;

int main(int argc, char const** argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, arr[MAX];
    std::cin >> n;
    for(int i = 0; i < n; i++) 
        std::cin >> arr[i];
    int minn = 1000000000;
    for(int i = 0; i < n; i++){ 
        int sum = 0;
        for(int j = 0; j < n; j++){ 
            sum += arr[j]*abs(i-j)*2;
            sum += arr[j]*i*2;
            sum += arr[j]*j*2;
        }
        if(sum < minn)
            minn = sum;
    }
    std::cout << minn << std::endl;
    return 0;
}


Codeforces-1077C - Good Array

题目链接

题目大意

就是给你一个数组(n个数),要你从这n个数中除掉一个,然后其余的n-1个数,刚好可以组成一个Good ArrayGood Array是指其中一个数刚好是其他所有数的和。要你求出所有可以去除的数的下标。

在这里插入图片描述

解析

对数组求一个和,并对数组排序(这样可以找到那个最大的数):

  • 使用一个结构体记录数组的值和对应的下标(等会排序下标就没了),然后用之前求的和sum,减去当前遍历的数,看这个结果 是否等于 2*排序之后最大的数
  • 要记得单独考虑排序之后最后那个数(也就是最大的那个数);
#include <bits/stdc++.h>

const int MAX = 200001;
typedef long long ll;

class Pair{ 
public:
    int id;
    int val;
    bool operator < (const Pair&thr)const{ 
        return this->val < thr.val;
    }
};

//bool cmp(const Pair& pa, const Pair& pb){ 
//    return pa.val < pb.val;
//}

int main(int argc, char const ** argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, res[MAX];
    Pair pr[MAX];
    std::cin >> n;
    ll sum = 0;
    for(int i = 0; i < n; i++){
        std::cin >> pr[i].val;
        pr[i].id = i;
        sum += pr[i].val;
    }
    //std::sort(pr, pr+n, cmp);
    std::sort(pr, pr+n);
    int count = 0;
    for(int i = 0; i < n-1; i++){ 
        ll tmp = sum;
        tmp -= pr[i].val;
        if(tmp == 2*pr[n-1].val) 
            res[count++] = pr[i].id+1;
    }
    if(sum - pr[n-1].val == 2*pr[n-2].val)
        res[count++] = pr[n-1].id+1;
    std::cout << count << std::endl;
    if(count > 0){ 
        for(int i = 0; i < count-1; i++)
            std::cout<< res[i] << " ";
        std::cout<< res[count-1] << std::endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zxzxzx0119/article/details/84997608