1056 Mice and Rice(25 分)

Mice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible in order to become a FatMouse.

First the playing order is randomly decided for N​P​​ programmers. Then every N​G​​ programmers are grouped in a match. The fattest mouse in a group wins and enters the next turn. All the losers in this turn are ranked the same. Every N​G​​ winners are then grouped in the next match until a final winner is determined.

For the sake of simplicity, assume that the weight of each mouse is fixed once the programmer submits his/her code. Given the weights of all the mice and the initial playing order, you are supposed to output the ranks for the programmers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N​P​​ and N​G​​ (≤1000), the number of programmers and the maximum number of mice in a group, respectively. If there are less than N​G​​ mice at the end of the player's list, then all the mice left will be put into the last group. The second line contains N​P​​ distinct non-negative numbers W​i​​ (i=0,⋯,N​P​​−1) where each W​i​​ is the weight of the i-th mouse respectively. The third line gives the initial playing order which is a permutation of 0,⋯,N​P​​−1 (assume that the programmers are numbered from 0 to N​P​​−1). All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the final ranks in a line. The i-th number is the rank of the i-th programmer, and all the numbers must be separated by a space, with no extra space at the end of the line.

Sample Input:

11 3
25 18 0 46 37 3 19 22 57 56 10
6 0 8 7 10 5 9 1 4 2 3

Sample Output:

5 5 5 2 5 5 5 3 1 3 5

题目大意:

//看了一遍题意,愣是没看懂。看了三遍还是没看懂,放弃了。看了题解上说的题意,还不不太明白,算了吧,看代码吧。

代码来自: https://www.liuchuo.net/archives/2936

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
struct node {
int weight, index, rank, index0;
};
bool cmp1(node a, node b) {
return a.index0 < b.index0;
}
int main() {
int n, g, num;
scanf("%d%d", &n, &g);
vector<int> v(n);
vector<node> w(n);
for(int i = ; i < n; i++)
scanf("%d", &v[i]);
for(int i = ; i < n; i++) {
scanf("%d", &num);
w[i].weight = v[num];//这个存的是体重
w[i].index = i;//这个存的是第几个,这个是对应老鼠编号的。
w[i].index0 = num;//这个存的是初始顺序。
}
queue<node> q;
for(int i = ; i < n; i++)
q.push(w[i]);
while(!q.empty()) {//目的是找出最胖的。
int size = q.size();
if(size == ) {
node temp = q.front();
w[temp.index].rank = ;
break;
}
int group = size / g;
if(size % g != )//这样来安排最后鼠数几个不够g的。
group += ;
node maxnode;
int maxn = -, cnt = ;
for(int i = ; i < size; i++) {
node temp = q.front();
w[temp.index].rank = group + ;
q.pop();
cnt++;
if(temp.weight > maxn) {
maxn = temp.weight;
maxnode = temp;
}
if(cnt == g || i == size - ) {
cnt = ;
maxn = -;
q.push(maxnode);
}
}
}
sort(w.begin(), w.end(), cmp1);
for(int i = ; i < n; i++) {
if(i != ) printf(" ");
printf("%d", w[i].rank);
}
return ;
}

//还是不太懂什么意思,以后再说。

PAT 1056 Mice and Rice[难][不理解]的更多相关文章

  1. PAT 1056 Mice and Rice

    #include <cstdio> #include <climits> #include <cstdlib> #include <vector> #i ...

  2. pat 甲级 1056. Mice and Rice (25)

    1056. Mice and Rice (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice an ...

  3. PAT 甲级 1056 Mice and Rice (25 分) (队列,读不懂题,读懂了一遍过)

    1056 Mice and Rice (25 分)   Mice and Rice is the name of a programming contest in which each program ...

  4. PAT Advanced 1056 Mice and Rice (25) [queue的⽤法]

    题目 Mice and Rice is the name of a programming contest in which each programmer must write a piece of ...

  5. 1056. Mice and Rice (25)

    时间限制 30 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice and Rice is the name of a pr ...

  6. 1056 Mice and Rice (25分)队列

    1.27刷题2 Mice and Rice is the name of a programming contest in which each programmer must write a pie ...

  7. PAT甲题题解-1056. Mice and Rice (25)-模拟题

    有n个老鼠,第一行给出n个老鼠的重量,第二行给出他们的顺序.1.每一轮分成若干组,每组m个老鼠,不能整除的多余的作为最后一组.2.每组重量最大的进入下一轮.让你给出每只老鼠最后的排名.很简单,用两个数 ...

  8. PAT (Advanced Level) 1056. Mice and Rice (25)

    简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...

  9. 【PAT甲级】1056 Mice and Rice (25 分)

    题意: 输入两个正整数N和M(<=1000),接着输入两行,每行N个数,第一行为每只老鼠的重量,第二行为每只老鼠出战的顺序.输出它们的名次.(按照出战顺序每M只老鼠分为一组,剩余不足M只为一组, ...

随机推荐

  1. STM32学习之路之MDK安装篇

  2. html的初识

    今天我们学习了Html语言,感觉学习这个是我期望很久的啦,之前在百度上面也看过html教程,但是看过之后也忘记啦,太多需要记忆的,所以也没记得什么啦.甚是遗憾啊,总感觉html需要学习好多东西啦的,但 ...

  3. 《C++ Primer Plus》12.6 复习各种(类和动态内存分配的)技术 笔记

    12.6.1 重载<<运算符要重新定义<<运算符,以便将它和cout一起用来显示对象的内容,请定义下面的友元运算符函数:ostream & operator<&l ...

  4. PHP的函数-----生成随机数、日期时间函数

    常用的函数 [1]   生成随机数 rand(); 例子: echo rand(); 显示结果: 当刷新时,会有不同的数,默认生成随机数.生成随机数不能控制范围. 如果,想要控制在范围之内,就用: e ...

  5. IOS strong和weak的区别

    strong和weak的区别 strong表示保留它指向的堆上的内存区域不再指向这块区域了. 也就是说我强力指向了一个区域,我们不再指向它的条件只有我们指向nil或者我自己也不在内存上,没有人stro ...

  6. ubuntu 安装nfs 服务

    1. 安装nfs服务软件 (1)服务器端 $sudo apt-get install nfs-kernel-server  rpcbind (2)客户端 (可以省略) $sudo apt-get in ...

  7. java基础----->TCP和UDP套接字编程

    这里简单的总结一下TCP和UDP编程的写法,另外涉及到HttpUrlConnection的用法 . TCP套接字 一.项目的流程如下说明: .客户输入一行字符,通过其套接字发送到服务器. .服务器从其 ...

  8. CVE-2017-1000117命令注入验证

    首先,我们来看第一个问题,就是ssh的一种操作. ssh -oProxyCommand=gnome-calculator xxx 问题的本质在于ssh会把//后的host那么部分带-号的按照参数指令去 ...

  9. 项目中启动另外的一个app

    NSMutableString *webViewContent = [[NSMutableStringalloc] init]; [webViewContent appendString:@" ...

  10. datatables如何让某个列中的值居中显示?

    https://datatables.net/reference/option/columns.className 通过 columns.className 属性设置: 例如: js: columns ...