主题:这道题目可以有更优化的解法吗?
http://acm.zju.edu.cn/show_problem.php?pid=2613
运行时间为00:00.18,我看上面有人用00:00.08 通过的,呵呵
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
const int size = 10001;
bool hasbid[size];
int num[size];
struct bid {
int price;
bool first;
char* name;
};
bool cmp(bid a, bid b)
{
if (num[a.price] == num[b.price])
{
if (a.price == b.price)
{
return !a.first;
}
return a.price > b.price;
}
return num[a.price] > num[b.price];
}
vector<bid> v;
main()
{
int T;
int n, m;
char name[50];
int price;
int i, time = 1;
scanf("%d", &T);
while (T --)
{
scanf("%d%d", &n, &m);
memset(hasbid, false, sizeof(bool) * (n + 1) );
memset(num, 0, sizeof(int) * (n + 1) );
v.clear();
for (i = 0; i < m; i ++)
{
scanf("%s%d", name, &price);
num[price] ++;
bid temp;
temp.price = price;
char* t_char = (char *)malloc(strlen(name) + 1);
strcpy(t_char, name);
t_char[strlen(name)] = '\0';
temp.name = t_char;
if (!hasbid[price])
{
temp.first = true;
hasbid[price] = true;
} else {
temp.first = false;
}
v.push_back(temp);
}
make_heap(v.begin(), v.end(), cmp);
printf("Case %d:\n", time ++);
printf("The winner is %s.\n", v[0].name);
printf("The price is %d.\n", v[0].price);
if(T)
printf("\n");
}
}