贪心算法

原题链接:http://acm.sgu.ru/problem.php?contest=0&problem=171

题目来源: http://acm.hust.edu.cn:8080/judge/contest/view.action?cid=11221#problem/B

CSUST-2012年暑假-8月11日,组队后第5场个人训练赛

第一次中奖题目,感谢kb神的代码。。。

一个多月后重做。

171. Sarov zones

time limit per test: 0.5 sec.
memory limit per test: 4096 KB
input: standard
output: standard
It is known that to participate the All-Russian Mathematic Olympiad one should do one of other olympiads enough good. These olympiads are called "zone olympiads" and the region from which people participate the same zone olympiad is called "zone". Sarov city of Nizhny Novgorod district is situated near the boundary of zones, so every year school students of Sarov participate several zones.

This year K zones invited students from Sarov to participate their olympiads. i-th zone invited N[i] students, so N=N[1]+N[2]+...+N[K] totally students were invited. After the Sarov city olympiad, N students were selected, so now the olympiad authorities of Sarov have to decide, which students will participate which zone olympiad. Each student is characterized by his olympiad level and each zone is characterized by its zone level. If a student with olympiad level P participates zone with zone level Q, he will be invited to All-Russian olympiad if and only if P>Q.

Every student is also characterized by his "weight". The Sarov olympiad authorities want, that the All-Russian Olympiad will be participated by students with maximal summary weight. You are to help them. Write a program which will divide the students between zones. Keep in mind that exactly N[i] students can go to i-th zone.


Input
On the first line of input there is an only integer K (1<=K<=100). On the second line there are K integers N[1], N[2], ... ,N[K] (0<=N[i]<=16000). On the third line there are K more integers Q[1], Q[2], ... ,Q[K] --- the zone levels of the zones. On the fourth line there are N integers P[1], P[2], ... P[N] --- olympiad levels of the students. (0<=Q[i]<=1000000, 0<=P[i]<=1000000). On the last (and fifth) line there are N integers w[1], w[2], ... w[k] --- the "weights" of students. 0<=w[i]<=100000. It is also guaranteed that 0<=N<=16000.

Output
Output only N integers --- Z[1], Z[2], ... Z[N] --- the numbers of zones which should be participated by students 1, 2, ... N.

Sample test(s)

Input
2 1 1 4 1 2 3 2 1
Output
2 1

题目大意:

有K个区域要在一个学校选拔学生去参加奥赛。
样例的第一行就是输入的区域个数。
每个区域有各自的限定名额,和进入的分数线,即题目中的zone level Q。给你要参赛的人的成绩和体重,让你把他们分别分到各个区域。
那么样例的第二行就有K个数,分别代表第一到第K个区域限定要选的名额。
样例的第三行也有K个数,分别对应进入相应区域的分数线Q(难度系数)。
样例第四行,各个学生的成绩。
样例第五行,对应的学生的体重。
分配好学生,并且输出各个区域参赛的学生编号。

关于输出:是按照输入时的学生顺序依次输出他们所在赛区的编号。

注意:

Keep in mind that exactly N[i] students can go to i-th zone
每一个区域都是要有N[i]个学生考试
两次都是先按照学生选了,再遍历区域...因为这里坑了很久Orz

叶:可类比于高考的录取,先划分录取,如果没有录满,则降分录取(即对于没有进入赛区的同学随机分配其实也是按照体重优先了)。

算法:贪心、sort()排序。

分析:

可以将区域按级别排序 ,从高到低依次处理。能够达到高级别区域的学生就放进去,一旦都达到了分数线,如果学生有多余,则优先考虑体重大的(学生体重从大到小排序),因为题目中奥组委要求的是希望各自的区域的选手,体重尽可能的大,所以处理当前级别时,一定优先考虑体重大的。最后剩下没有放入任何区域的学生属于不起作用的,随机放入还有名额空闲的区域即可[由于开始按照体重排序好了,所以这里还是默认了优先选的体重大的了]。

两个排序:

1、先对区域的录取分数排序,当然是择优录取,但是因为每个学生都必须要有地方比赛、所以先对区域的分数由大到小排序。
2、再对学生排序,学生的特征有两点一个是成绩,另一个是体重。因为题目要求的是各奥组委希望自己赛区的选手的体重尽量的大,所以先考虑学生的体重,按照体重从大到小排序,再按照成绩的由小到大排序。如果体重相同(但是数据中好像没有涉及到体重相同的情况,没考虑这个,代码也A了,奇怪的题目。),则先考虑p小的,毕竟还是尽量让每个学生都达到录取线,而p大的话,选择的机会也大,所以体重相同是优先考虑p小的,当然这一点对于AC貌似没有关系。

重做了下,发现按照分数排序出现了很多问题,各种格式错误,那么就不按照分数排序了,直接按照重量从大到小排序 ,依次遍历全部的学生,只要达到了分数线录取即可.由于开始是先排序了体重,所以后面选的时候也默认了先选体重大的,合题意.


最后如果把每个学生所在的考试区域写进了结构体,输出时还要先按照学生的编号排序下.

关于PE的坑希望路过的大神解释下:

首先题目要求是单组输入的,但是写多了,习惯性多组输入了Orz
下面我自己的这两个代码测试了下:
多组输入:

没空格,有换行 AC
有空格,有换行 AC

没空格,没换行 AC
有空格,没换行 AC

单组输入:

没空格,有换行  AC
没空格,没换行 AC

有空格,有换行 AC
有空格,没换行 AC

但是别人的也是这么写的,有的最后加个空格就过了 ,有的把下标从 0 开始改到从 1 开始就过了,开始比赛时自己也各种PE错误,PE on test 2PE on test 5 ... 还是不知道怎么会有PE

2013 暑假重做代码

/*
Accepted    1075 KB 46 ms   Visual Studio C++ 2010    2050 B
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;const int maxQ = 110;
const int maxP = 16000+10;struct Qone{int index;int level;int studentNum;
}Q[maxQ];struct People{int index;int level;int weight;int qone;
}P[maxP];bool cmp1(Qone A, Qone B)
{return A.level > B.level;
}
bool cmp2(People A, People B)
{return A.weight > B.weight;
}
bool cmp3(People A, People B)
{return A.index < B.index;
}
int main()
{int K, N;while(scanf("%d", &K) != EOF){N = 0;for(int i = 1; i <= K; i++){scanf("%d",&Q[i].studentNum);Q[i].index = i;N += Q[i].studentNum;}for(int i = 1; i <= K; i++){scanf("%d", &Q[i].level);}sort(Q+1,Q+K+1,cmp1); //Level Max to minfor(int i = 1; i <= N; i++){scanf("%d", &P[i].level);P[i].index = i;P[i].qone = 0;}for(int i = 1; i <= N; i++){scanf("%d", &P[i].weight);}sort(P+1, P+N+1, cmp2); //Weight Max to minfor(int i = 1; i <= N; i++) //先按照达到 level 的选{for(int j = 1; j <= K; j++){if(Q[j].studentNum > 0){if(P[i].level > Q[j].level){P[i].qone = Q[j].index;Q[j].studentNum--;break;}}}}int j = 1;for(int i = 1; i <= N; i++) // 按照体重由大到小随意分配剩下的学生,保证每个学生都有考试区域{if(P[i].qone != 0) continue;while(Q[j].studentNum == 0) j++;P[i].qone = Q[j].index;Q[j].studentNum--;}sort(P+1, P+1+N, cmp3);for(int i = 1; i <= N; i++){if(i == 1) printf("%d", P[i].qone);else printf(" %d", P[i].qone);}printf("\n");}return 0;
}

2012重做总结代码

//Accepted   1139 KB 46 ms   Visual Studio 8 C++   1338 B  2012-09-22 15:44:41
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXK=100+10;
const int MAXN=16000+10;
struct Zone
{int index;//编号int N;//需要的人数int Q;//进入该区域的分数线
}zone[MAXK];
struct Stu
{int index;//编号int P;//水平int w;//重量
}stu[MAXN];
int p[MAXN];
bool cmp1(Zone a,Zone b)//按照Q从大到小排序
{return a.Q>b.Q;
}
bool cmp2(Stu a,Stu b)//按照w从大到小排序
{return a.w>b.w;
}
int main()
{int k;int i,j;int num=0;scanf("%d",&k);for(i=0;i<k;i++){zone[i].index=i+1;scanf("%d",&zone[i].N);num+=zone[i].N;}for(i=0;i<k;i++)scanf("%d",&zone[i].Q);sort(zone,zone+k,cmp1);for(i=0;i<num;i++){stu[i].index=i+1;scanf("%d",&stu[i].P);}for(i=0;i<num;i++)scanf("%d",&stu[i].w);sort(stu,stu+num,cmp2);memset(p,-1,sizeof(p));for(i=0;i<num;i++)//第一轮先分配成绩达到了的{for(j=0;j<k;j++){if(stu[i].P>zone[j].Q && zone[j].N)//注意:是stu[i].P>zone[j].Q不是>={p[stu[i].index]=zone[j].index;zone[j].N--;break;//依次对排好序的学生逐个从排好序的区域逐个安排}//也就是说对下一个学生也从第一个区域开始排}}j=0;for(i=0;i<num;i++)//分配剩下的人{if(p[i+1]!=-1)continue;//表示已经分配while(zone[j].N==0)j++;//表示当前区域已经招满p[i+1]=zone[j].index;zone[j].N--;}for(i=1;i<=num;i++)printf("%d ",p[i]);return 0;
}

SGU 171 Sarov zones【贪心】相关推荐

  1. 史上最全的SGU题目分类

    由于SGU上神题遍地,特列此表,便于训练时分类训练. 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traffic Lights 最短路 104 Little Shop ...

  2. 华为机试python编程题_牛客网华为机试题之Python解法

    牛客网华为机试题之Python解法 第1题 字符串最后一个单词的长度 a = input().split(" ") print(len(a[-1])) 第2题 计算字符个数 a = ...

  3. SGU 325 Palindrome(贪心)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=325 题意:给出一个字符串.每次可以交换相邻的两个字母.问最少需要多少次可以使得串变 ...

  4. [2018.04.17][水][日志][6][#171~#181][贪心算法][已经丧心病狂][背景-amp;amp;amp;amp;gt;][最虚伪的算法]

    [背景]     在某日和某大佬们一起举行的ACM上,我们小队面对一道多重背包题跪了...凉凉(可喜的是我AC了C,G两题).     在面对大佬们的D题上,我们使用了,啊不,第一次使用了贪心算法,奇 ...

  5. 171. Leetcode 406. 根据身高重建队列 (贪心算法-两个维度权衡题目)

    class Solution:def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:people.sort ...

  6. HDU4846Task treap + 贪心

    题意:给你 n台机器,m个任务,机器有两个属性值  一天最多工作时间X,机器等级 Y  ,任务有两个属性值   需要机器的工作时间X,需要机器的工作等级 Y ,每台机器一天只能做一个任务,每做一个任务 ...

  7. java基础(二) 自增自减与贪心规则

    戳上面的蓝字关注我们哦! 精彩内容 精选java等全套视频教程 精选java电子图书 大数据视频教程精选 java项目练习精选 引言   JDK中提供了自增运算符++,自减运算符--.这两个操作符各有 ...

  8. (Step2-500题)POJ训练计划+SGU

    经过Step1-500题训练,接下来可以开始Step2-500题,包括POJ训练计划的298题和SGU前两章200题.需要1-1年半时间继续提高解决问题和编码实现能力,加油ACMer!任重道远 Ste ...

  9. SGU 286 Ancient decoration(Euler路径+二分匹配)

    http://acm.sgu.ru/problem.php?contest=0&problem=286 先找欧拉回路,再做二分匹配,输出匹配 有一道题和这个很像:HDU 3551 Hard P ...

最新文章

  1. 请给出一个Scala RDD的HelloWorld例子
  2. openStack controller 管理网口TX数据量非常大 网络总是丢包
  3. np.dot()函数用法(亲测矩阵算法)
  4. 激活函数:sigmoid、Tanh、ReLU
  5. jdk的Selector(3)select的过程
  6. Kernel Method核方法—基本概念
  7. asio几种异步编程模型
  8. 冒死解密,微信逆向:破解聊天记录文件!
  9. [微服务]API 路由管理--Gateway网关
  10. 餐饮店如何做活动吸引人
  11. 关于高版本web3j调用okhttp3.RequestBody.create(Ljava/lang/String;Lokhttp3/MediaType;)Lokhttp3/RequestBody异常
  12. 什么是 DORA 指标以及它们如何告知 DevOps 成功?
  13. JVM G1 源码分析(七)- Full GC
  14. 学习进度总结————王烁130201218
  15. 深度学习与“免费”GPU
  16. 机顶盒显示该服务器未授权,户户通提示E04该频道未授权的解决办法
  17. 初学RUST-让程序跑起来
  18. ctfshow摆烂杯
  19. 短视频App源码:如何搭建短视频社区
  20. 教育邮箱怎么申请?国际教育电子邮箱

热门文章

  1. python大括号_如何在python字符串中打印文字大括号字符并在其上使用.format?
  2. RT-Thread学习笔记——PIN 设备
  3. 第 5 篇:用视图集,简化你的代码
  4. 话说QQ插件的精简概要
  5. 阿里云OSS图片生成缩略图和获取视频的封面方法
  6. arc hdmi 接线图_HDMI ARC功能详解及应用介绍
  7. Python调用笔记本摄像头,并实现人脸检测功能
  8. 【数据结构】【王道408】——PPT截图与思维导图
  9. flutter 卡顿_Flutter渲染性能优化全攻略(解决应用卡顿)
  10. [L1 - 5分合集]后天