题意:先给定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;
}

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

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

    Little Vova studies programming to p. Vova is very smart and he can write every test for any mark, b ...

  2. CodeForces - 540B School Marks —— 贪心

    题目链接:https://vjudge.net/contest/226823#problem/B Little Vova studies programming in an elite school. ...

  3. CodeForces 540B School Marks

    http://codeforces.com/problemset/problem/540/B School Marks Time Limit:2000MS     Memory Limit:26214 ...

  4. codeforces 540B.School Marks 解题报告

    题目链接:http://codeforces.com/problemset/problem/540/B 题目意思:给出 k 个test的成绩,要凑剩下的 n-k个test的成绩,使得最终的n个test ...

  5. CodeForces 540B School Marks(思维)

    B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  7. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  8. Codeforces 161 B. Discounts (贪心)

    题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...

  9. CodeForces 176A Trading Business 贪心

    Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a ...

随机推荐

  1. 启动 Eclipse 报错 “An internal error occurred during: "Initializing Java Tooling". java.lang.NullPointerException”

    之前在线升级了Eclipse,由于网络/或者是设置问题,在升级完成后启动Eclipse出线上述错误... 解决方法 1. 删除目录工作目录下面的.project文件夹: 如下图: 2. 关闭Eclip ...

  2. @BindingAnnotation

    (1) 如果 一个接口只有一个实现,使用这种连接注解就可以: bind(XXInterface.class).to(XXImpl.class); @Inject XXInterface xxInter ...

  3. Cocos2d-html5帧动画

    单独获取plist里面一个文件: cc.SpriteFrameCache.getInstance().addSpriteFrames(s_test_plist); var spriteTest2 = ...

  4. 在linux下使用debugfs恢复rm删除的文件

    原理主要是删除的文件并没有实际上从硬盘上摸去,只是inode索引删除了相关的信息,因此只要找到刚删除文件的block上,就可以恢复已经删除的文件. 以下方法在ext3的文件系统上测试通过,ext2的没 ...

  5. OD 实验(一) - 修改程序标题

    需要修改的程序 把 I love fishc.com 修改为 hello world sch01ar 用 OD 打开程序 在程序入口处开始一直按 F8 运行程序,看看在哪里弹出对话框 运行到该地址的时 ...

  6. django之分页器

    view from django.shortcuts import render,HttpResponse # Create your views here. from app01.models im ...

  7. 热门数据挖掘模型应用入门(一): LASSO回归

    热门数据挖掘模型应用入门(一): LASSO回归 2016-10-10 20:46 作者简介: 侯澄钧,毕业于俄亥俄州立大学运筹学博士项目, 目前在美国从事个人保险产品(Personal Line)相 ...

  8. Rhythmk 一步一步学 JAVA (14) Spring-3 @Autowired,@Qualifier @Required @Resource @Component,@Service,@Controller,@Repository @PostConstruct,@PreDestroy

    1.@Autowired 注解:首先在使用时候需要引入配置: <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 --> ...

  9. Redis实战——Redis的pub/Sub(订阅与发布)在java中的实现

    借鉴:https://blog.csdn.net/canot/article/details/51938955 1.什么是pub/sub Pub/Sub功能(means Publish, Subscr ...

  10. Oracle在linux中相关设置操作

    set linesize 300;  -- 设置行长度 set pagesize 300; set long 100000; -- 设置输出长度select dbms_metadata.get_ddl ...