CodeForces 540B School Marks (贪心)

题意:先给定5个数,n,  k, p, x, y。分别表示 一共有 n 个成绩,并且已经给定了 k 个,每门成绩 大于0 小于等于p,成绩总和小于等于x,

但中位数大于等于y。让你找出另外的n-k个成绩。

析:利用贪心算法,首先是只能小于等于 p,也就是成绩越小越好, 然后中位数还得大于等于y,所以我们放的成绩只有两个,一个是1,另一个是y,那么是最优的,

所以每次只要判定这个就好,在判定的时候,要先排序,再找中位数,再就是每次放两个,如果n-k不是偶数,那么就放一个数,再进行。

代码如下:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
vector<int> ans;
int a[1005];

int main(){
    int n, m, p, x, y, k;
    scanf("%d %d %d %d %d", &n, &k, &p, &x, &y);
    int sum = 0;
    for(int i = 0; i < k; ++i){
        scanf("%d", &a[i]);
        sum += a[i];
    }
    int ds = x - sum;
    int dn = n - k;
    bool ok = true;
    if(dn & 1){
        sort(a, a+k);
        if(a[k/2] >= y){
            ans.push_back(1);
            a[k++] = 1;
            ds--;
        }
        else{
            ans.push_back(y);
            a[k++] = y;
            ds -= y;
        }
        dn = n - k;
    }

    for(int i = 0; i < dn; i += 2){
        sort(a, a+k);
        int mid = a[k/2];
        if(mid < y){
            ans.push_back(y);
            ans.push_back(y);
            ds -= y * 2;
            a[k++] = y;
            a[k++] = y;
        }
        else {
            a[k++] = 1;
            ans.push_back(1);
            a[k++] = 1;

            sort(a, a+k);
            if(a[k/2] < y){
                a[0] = y;
                ds -= y + 1;
                ans.push_back(y);
            }
            else{  ds -= 2;
                ans.push_back(1);
             }
        }
    }

    sort(a, a+k);
    if(a[k/2] < y || ds < 0)  printf("-1");
    else for(int i = 0; i < ans.size(); ++i)
            if(!i)  printf("%d", ans[i]);
            else printf(" %d", ans[i]);
    printf("\n");

    return 0;
}
时间: 2024-10-23 01:27:49

CodeForces 540B School Marks (贪心)的相关文章

(CodeForces )540B School Marks 贪心 (中位数)

Little Vova studies programming in an elite school. Vova and his classmates are supposed to write n progress tests, for each test they will get a mark from 1 to p. Vova is very smart and he can write every test for any mark, but he doesn't want to st

CodeForces 540B School Marks

http://codeforces.com/problemset/problem/540/B School Marks Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 540B Description Little Vova studies programming in an elite school. Vova and

cf 540b School Marks 贪心

B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little Vova studies programming in an elite school. Vova and his classmates are supposed to write n progress tests, for each

Codeforces Round #300——C贪心——Tourist&#39;s Notes

Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, me

Codeforces 77C 树形dp + 贪心

题目链接:点击打开链接 题意: 给定n个点, 每个点的豆子数量 下面是一棵树 再给出起点 每走到一个点,就会把那个点的豆子吃掉一颗. 问:回到起点最多能吃掉多少颗豆子 思路:树形dp 对于当前节点u,先把子节点v都走一次. 然后再往返于(u,v) 之间,直到u点没有豆子或者v点没有豆子. dp[u] 表示u点的最大值.a[u] 是u点剩下的豆子数. #include <cstdio> #include <vector> #include <algorithm> #inc

Codeforces 788A Functions again - 贪心

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about

Codeforces 486C Palindrome Transformation(贪心)

题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N,指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:其实只要是对称位置不相同的,那么指针肯定要先移动到这里,修改字符只需要考虑两种方向哪种更优即 可.然后将所有需要到达的位置跳出来,贪心处理. #include <cstdio> #include <cstring> #include <cstdlib> #include <vec

codeforces 735C Tennis Championship(贪心+递推)

Tennis Championship 题目链接:http://codeforces.com/problemset/problem/735/C --每天在线,欢迎留言谈论. 题目大意: 给你一个 n (2≤n≤10^18),代表一共有n位参加比赛的选手. 游戏规则: ①每次比赛,输的选手将离开赛场 ②相互比赛的选手 他们的获胜的次数相差不能超过1(获胜4次的选手只能跟3或5次的选手比赛) 问题:最终赢得比赛的选手,胜场最多能为多少. 思路: 贪心:①选一名选手让他一直获胜且优先让他参加比赛 ②当

CodeForces 545C Woodcutters (贪心orDP)

[题目链接]:click here~~ [题目大意]: 有n棵树,给出每棵树的位置和高度,然后把树砍掉,树可以向左倒也可以向右倒.输出最多能砍几棵树. [思路]:利用贪心的思想.第一棵树的左边和最后一棵树的右边没树,所以他们向两边倒,然后对于中间的树来说,首先先向左边倒,然后左边距离如果不够的话再向右边倒,向右倒的时候注意更新一下距离. 代码: /* * Problem: CodeForces 545C * Running time: 46MS * Complier: G++ * Author: