| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
又被输入坑了,总WA的可以看一下输入不保证id排序!
输入不保证id排序!
输入不保证id排序!
重要的事情说三遍!!!
讲一下我的思路。每一条Query我都会记录一个lefttime,即这个Query还差多长时间就该响应了。
每次找出lefttime最小的,这个必然最先响应,然后跟新所有的lefttime到这个时间点。然后就是接下来的一轮,直到总共响应了k次。
写之前我估算过,时间复杂度为O(n*logn + n*k),带最大规模进去算,大概是10的7次方,而且我的循环体比较简单,应该可以通过。最后试试证明,47ms。
贴一下代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Query
{
int id; //id
int period; //响应周期
int lefttime; //剩余的时间
};
bool cmp(struct Query a,struct Query b)
{
return a.id < b.id;
}
int main()
{
int i, n, k, t1;
struct Query q[1000];
char input[100];
//读取指令,并计数
n = 0;
gets(input);
while(strcmp(input, "#")!=0)
{
//取出id号
t1 = 0;
for(i=9; input[i]!=' '; i++)
{
t1 *= 10;
t1 += input[i] - '0';
}
q[n].id = t1;
//取出period周期
t1 = 0;
for(i += 1; input[i]!='\0'; i++)
{
t1 *= 10;
t1 += input[i] - '0';
}
q[n].period = t1;
q[n].lefttime = t1;
n++;
gets(input);
}
//读取k
scanf("%d", &k);
//按id排序
sort(q, q+n, cmp);
//响应k次
while(1)
{
//找最小值的下标
t1 = q[0].lefttime;
for(i=1;i<n;i++)
{
if(q[i].lefttime < t1)
t1 = q[i].lefttime;
}
//修改lefttime,并响应
for(i=0;i<n;i++)
{
if(q[i].lefttime==t1)
{
printf("%d\n", q[i].id);
k--;
if(k==0)
return 0;
q[i].lefttime = q[i].period;
}
else
q[i].lefttime -= t1;
}
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator