雕刻时光

just do it……nothing impossible
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

贪心—— Zoj_3589

Posted on 2012-03-15 21:42  huhuuu  阅读(175)  评论(0编辑  收藏  举报

难点不在贪心,在于对字符串的处理,字符串处理为数字sscanf就可以了

View Code
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;

struct data
{
double x,y;
int time;
}s[100009];

double dis[100009];

double fdis(double x0,double y0,double x1,double y1)
{
double all=(x0-x1)*(x0-x1)+(y0-y1)*(y0-y1);
return sqrt(all);
}

int cmp(data a,data b)
{
return a.time<b.time;
}

int main()
{
char str[1009];
scanf("%s",str);
int k;
int end=0;

while(1)
{
int i;
int add=0;
scanf("%d",&k);
while(1)
{
if(scanf("%s",str)==EOF)
{
end=1;
break;
}
if(strcmp(str,"[HitObjects]")==0)
{
break;
}

sscanf(str,"%lf,%lf,%d,%*lf,%*lf",&s[add].x,&s[add].y,&s[add].time);
add++;
}

if(k==1)
{
printf("0.000\n");
}
else
{
sort(&s[0],&s[add],cmp);
for(i=0;i<add-1;i++)
{
dis[i]=fdis(s[i].x,s[i].y,s[i+1].x,s[i+1].y);
}
sort(&dis[0],&dis[add-1]);

printf("%.3lf\n",dis[add-k]);
}

if(end==1)
{
break;
}
}

return 0;
}