现在的位置: 首页 > 综合 > 正文

hdu 1052贪心算法

2013年08月24日 ⁄ 综合 ⁄ 共 496字 ⁄ 字号 评论关闭
//贪心算法
//稍有点麻烦,要分析一下各种情况
#include <iostream>
#include <algorithm>
using namespace std;

int n;
int Sp_A[1001];
int Sp_B[1001];
bool cmp(int x,int y)
{
     return x > y;
}

int main()
{
    while(cin>>n)
    {
          if(n == 0)
               break;
          for(int i = 0; i < n; i++)
          {
                  cin>>Sp_A[i];
          }
          for(int i = 0; i < n; i++)
          {
                  cin>>Sp_B[i];
          }

          sort(Sp_A,Sp_A+n,cmp);
          sort(Sp_B,Sp_B+n,cmp);

          int s1,s2,e1,e2;
          int k = 0;
          s1 = s2 = 0;
          e1 = e2 = n-1;
          for(;s1 <= e1;)
          {
                  if(Sp_A[e1] > Sp_B[e2])
                  {
                         k++;
                         e1--;
                         e2--;     
                  }
                  else if(Sp_A[s1] > Sp_B[s2])
                  {
                         k++;
                         s1++;
                         s2++;
                  }
                  else
                  {
                         if(Sp_A[e1]!=Sp_B[s2])
                         {
                               k--;

                         }
                          e1--;
                          s2++;
                  }
          }          
          cout<<200*k<<endl;
    }
    return 0;
}
 

抱歉!评论已关闭.