ArcTan

dfs
随笔 - 16, 文章 - 117, 评论 - 6, 引用 - 0
数据加载中……

hdu1052(贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=1052

尼玛的贪心算法:
#include<stdio.h>


int qsort(int x[],int s,int t)
{
    
int i,j,mid,tmp;
    i
=s;
    j
=t;
    mid
=x[(i+j)/2];
    
while (i<=j)
    {
        
while (x[i]>mid)    i++;
        
while (x[j]<mid)    j--;
        
if (i<=j)
        {
            tmp
=x[i];
            x[i]
=x[j];
            x[j]
=tmp;
            i
++;
            j
--;
        }
    }
    
if (s<j)    qsort(x,s,j);
    
if (i<t)    qsort(x,i,t);
}
int main()
{
    
int n,i,i1,i2,j1,j2,ans,a[1003],b[1003];
    
while (scanf("%d",&n)==1&&n!=0)
    {
        
for (i=1; i<=n; i++)
            scanf(
"%d",&a[i]);
        qsort(a,
1,n);
        
for (i=1; i<=n; i++)
            scanf(
"%d",&b[i]);
        qsort(b,
1,n);   //尼玛,这里我复制过来的,qsort(a,1,b);,,wa了我数十次啊!!!!!!

        ans
=0;
        i1
=1;
        j1
=n;
        i2
=1;
        j2
=n;
        
while (i1<=j1)
        {
            
if (a[j1]>b[j2])
            {
                ans
++;
                j1
--;j2--;
                
continue;
            }
            
else
                
if (a[j1]<b[j2])
                {
                    ans
--;
                    j1
--;i2++;
                    
continue;
                }
            
if (a[i1]>b[i2])
            {
                ans
++;
                i1
++;i2++;
                
continue;
            }
            
else
                
if (a[i1]<b[i2])
                {
                    ans
--;
                    j1
--;i2++;
                    
continue;
                }
                
else
                    
if (a[j1]<b[i2])
                    {
                        ans
--;
                        j1
--;i2++;
                    }
                    
else
                    {
                        j1
--;i2++;
                    }
        }
        printf(
"%d\n",ans*200);
    }
    
return 0;
}


posted on 2012-03-12 22:11 wangs 阅读(382) 评论(0)  编辑 收藏 引用 所属分类: ACM-201203


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理