您的位置:首页 > 其它

zoj - 2511 - Design T-Shirt

2012-12-10 20:07 302 查看
题意:N个人为M个elements投票,计算各个element的票数和,选出最高的K个,逆序输出下标。

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1511

——>>题目本来不难,用优先队列来获取票数最高的K个下标即可,不料,一个疏忽,重载 < 时少了一个条件——在票数相同时选择下标较小的那个……WA了2次!!!

#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

const int maxn = 10000 + 10;        //最大element数

struct node     //结点数据类型
{
int indice;     //element的下标,最小为1
double sum;     //该element得到的总分数
node():sum(0){}     //初始化为0
};

bool operator < (node e1, node e2)      //定义优先队列的排序方式,注意别漏了第二个条件!!!
{
return ((e1.sum < e2.sum) || (e1.sum == e2.sum && e1.indice > e2.indice));
}

int main()
{
int N, M, K, i, j;
double temp;
node *element;      //不知道最大有几个element,开个动态的数组来存

while(cin>>N>>M>>K)
{
element = new node[M+1];        //申请空间
for(i = 1; i <= N; i++)
for(j = 1; j <= M; j++)
{
cin>>temp;
element[j].indice = j;      //保存各个element的下标
element[j].sum += temp;     //累加各个element的分数和
}

priority_queue<node> qu;        //程序的核心,优先队列

for(i = 1; i <= M; i++)     //各个element入列
qu.push(element[i]);

int id[maxn];       //用来存选出来的K个elements的下标
for(i = 1; i <= K; i++)
{
id[i-1] = qu.top().indice;      //取选出来的K个elements的下标
qu.pop();
}
sort(id, id+K);     //排序
for(i = K-1; i > 0; i--)    //逆序输出
cout<<id[i]<<" ";
cout<<id[0]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: