| ||||||||||
| 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 | |||||||||
Re:提供思路! 和比较容易错的情况!自己写了个快排+贪心的!附代码!!!!(仅供参考)纪念水过100!In Reply To:提供思路! 和比较容易错的情况!自己写了个快排+贪心的!附代码!!!!(仅供参考) Posted by:2007210562 at 2009-08-11 21:06:44 > 首先,是对输入数据快排,最好定义个结构体! 先按Start从小到大排序,如果相等,再按二维的从大到小排序。
> 第二步,首先判断,s[0].start==1 ? 然后判断s[0].end==t?(我就是倒在这个地方,贡献几个wa!) 最后开始按贪心来做。每次找到,满足Start,在原来的基础上,end去的最大值,直到找到t为止,或者找不到,定义个tag标志表示能否找到!
>
> #include <iostream>
> #include <algorithm>
> using namespace std;
> struct Duan
> {
> int start;
> int end;
> };
> struct Duan s[25005];
> int comp (struct Duan a,struct Duan b)
> {
> return (a.start<b.start)||(a.start==b.start && a.end>b.end);
> }
>
> int main ()
> {
> int n,t,count,i,j,frist,second,tag,tag1;
> scanf ("%d%d",&n,&t);
> for (i=0;i<n;i++)
> scanf ("%d%d",&s[i].start,&s[i].end);
> sort(s,s+n,comp);
> if (s[0].start==1)
> {
> if (s[0].end==t)
> {
> printf ("1\n");
> }
> else
> {
> count=1;
> frist=second=s[0].end;
> j=0;
> tag=0;
> while (1)
> {
> tag1=0;
> for (i=1+j;i<n;i++)
> {
> if (s[i].start-1<=frist && second<s[i].end)
> {
> second=s[i].end;
> j=i;
> tag1=1;
> }
> }
> if (tag1==0)
> break;
> count++;
> if (second==t)
> {
> tag=1;
> printf ("%d\n",count);
> break;
> }
> frist=second;
>
> }
> if (tag==0)
> printf ("-1\n");
> }
> }
> else
> printf ("-1\n");
> return 0;
> }
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator