zoukankan      html  css  js  c++  java
  • codeforces 540B.School Marks 解题报告

    题目链接:http://codeforces.com/problemset/problem/540/B

    题目意思:给出 k 个test的成绩,要凑剩下的 n-k个test的成绩,使得最终的n个test之和 <= x,且中位数 >= y。凑的时候   1 <= test的成绩 <= p

          做得可辛苦了,泪~~~

      首先统计给出的 k 个test中有多少个是 >= y 的,然后从后往前补充,直到中位数是 k ,其余的数用 1 来填充。

      无解结果需要考虑清楚。(1)n-k+sum_k 是最小的和,如果比x小那么就无解;(2)凑的y的数目和、剩下填充1的和、sum_k三者之和 大于 x 。  (3)凑的y的数目比可填充的数量(n-k) 还要多也无解。

      还有一个小细节,凑的y的数目可能为负数,特判一下,负数的话用 0 替代。

        

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     #ifndef ONLINE_JUDGE
    10         freopen("in.txt", "r", stdin);
    11     #endif // ONLINE_JUDGE
    12 
    13     int n, k, p, x, y;
    14     while (scanf("%d%d%d%d%d", &n, &k, &p, &x, &y) != EOF) {
    15        int a;
    16        int sum = 0;
    17        int cnt = 0;
    18        for (int i = 1; i <= k; i++) {
    19             scanf("%d", &a);
    20             if (a >= y)
    21                 cnt++;
    22             sum += a;
    23        }
    24        int mid_p = (n+1)/2;      // 中位数位置
    25        int rem = n - k;
    26        int y_num = mid_p - cnt; // 补充y的个数,直到中位数是 y
    27        if (y_num * y + (rem-y_num) + sum > x || y_num > rem || n-k + sum > x)
    28             puts("-1");
    29        else {
    30             y_num = (y_num < 0 ? 0 : y_num);
    31             for (int i = 0; i < y_num; i++)
    32                 printf("%d ", y);
    33             for (int i = 0; i < rem-y_num; i++)
    34                 printf("%d%c", 1, i != rem-y_num-1 ? ' ' : '
    ');
    35        }
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    python的基本操作while循环体
    python中类的神奇方法应用案例
    Python中类的神奇方法
    python 中类的初始化方法
    Python中类的创建和self的作用
    投掷骰子的游戏,键值对
    字典常用方法
    剪刀石头布
    math 模块
    PYTHON内置模块
  • 原文地址:https://www.cnblogs.com/windysai/p/4471448.html
Copyright © 2011-2022 走看看