hdu1257最少拦截系统

又是一道坑爹的题目,明明数据都通过了,就是wrong anser

#include "iostream"
#include "string.h"
using namespace std;
struct {
  int x,index;
}dp[1000];
int find(int low,int high,int a){
  while(low<=high){
    int mid=(low+high)/2;
    if(dp[mid].x>=a)low=mid+1;
    else high=mid-1;
  }
  return low;
}
int main(){
  int n,i,num[1000],k,x,set[1000],len,longth;
  while(cin>>n){
    longth=n;
    for(i=1;i<=n;i++)cin>>num[i];
    memset(set,0,sizeof(set));
    for(k=1;;k++){
      memset(dp,0,sizeof(dp));
      x=1;
      while(set[x++]);
      dp[1].x=num[x-1];dp[1].index=x-1;set[x-1]=1;
      len=1;
      for(i=1;i<=n;i++){
        if(set[i]==0){
         int temp=find(1,len,num[i]);
         if(temp>len)len++;
         else {set[dp[temp].index]=0;}
         dp[temp].x=num[i];
         dp[temp].index=i;
         set[i]=1;
       }
      }
      //for(i=1;i<=len;i++)cout<<dp[i].x<<' ';cout<<endl;
      longth-=len;
      if(longth==0)break;
    }
    cout<<k<<endl;
  }
}

人家的代码

//1273712 2009-04-15 16:57:00 Accepted 1257 0MS 280K 659 B C++ Xredman
#include <iostream>
#include <cstdio>
using namespace std;

const int N = 10000;

int str[N];

int main()
{
    int cnt, n;
    int a, i, j;
    int minv, tmin;
    while(cin>>n)
    {
        scanf("%d", &a);
        cnt = 1;
        str[0] = a;
        for(i = 1; i < n; i++)
        {
            scanf("%d", &a);
            tmin = minv = -1;
            for(j = 0; j < cnt; j++)
                if(str[j] - a  >= 0)
                {
                    if(tmin == -1)
                    {
                        minv = j;
                        tmin = str[j] - a;
                    }
                    else if(str[j] - a < tmin)
                    {
                        minv = j;
                        tmin = str[j] - a;
                    }
                }
            if(tmin == -1)
                str[cnt++] = a;
            else
                str[minv] = a;
         //for(j=0;j<cnt;j++)cout<<str[j]<<' ';cout<<endl;
        }

        cout<<cnt<<endl;
    }
    return 0;
}

 

 

posted @ 2013-08-28 10:13  龙城星  阅读(152)  评论(0编辑  收藏  举报