hdu 5058 So easy (排序+离散化)

So easy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 510    Accepted Submission(s): 285


Problem Description
Small W gets two files. There are n integers in each file. Small W wants to know whether these two files are same. So he invites you to write a program to check whether these two files are same. Small W thinks that two files are same when they have the same integer set.
For example file A contains (5,3,7,7),and file B contains (7,5,3,3). They have the same integer set (3,5,7), so they are same.
Another sample file C contains(2,5,2,5), and file D contains (2,5,2,3).
The integer set of C is (2,5),but the integer set of D is (2,3,5),so they are not same.
Now you are expected to write a program to compare two files with size of n.
 

Input
Multi test cases (about 100). Each case contain three lines. The first line contains one integer n represents the size of file. The second line contains n integers a1,a2,a3,,an - represents the content of the first file. The third line contains n integers b1,b2,b3,,bn - represents the content of the second file.
Process to the end of file.
1n100
1ai,bi1000000000
 

Output
For each case, output "YES" (without quote) if these two files are same, otherwise output "NO" (without quote).
 

Sample Input
  
  
3 1 1 2 1 2 2 4 5 3 7 7 7 5 3 3 4 2 5 2 3 2 5 2 5 3 1 2 3 1 2 4
 

Sample Output
  
  
YES YES NO NO
 

Source
题目大意:判断两个集合是否相等
题目分析:先排序,然后离散化
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 107

using namespace std;

int n,cnt1,cnt2;
int a[MAX],b[MAX];
int c[MAX],d[MAX];

int main ( )
{
    
    while ( ~scanf ( "%d" , &n ) )
    {
        for ( int i = 1 ; i <= n ; i++ )
            scanf ( "%d" , &a[i] );
        for ( int i = 1 ; i <= n ; i++ )
            scanf ( "%d" , &b[i] );
        sort ( a+1 , a+n+1 );
        sort ( b+1 , b+n+1 );
        cnt1 = cnt2 = 0;
        a[0] = b[0] = -1;
        for ( int i = 1 ; i <= n ; i++ )
            if ( a[i] != a[i-1] )
                c[cnt1++] = a[i];
        for ( int i = 1 ; i <= n ; i++ )
            if ( b[i] != b[i-1] )
                d[cnt2++] = b[i];
        bool flag = true;
        if ( cnt1 == cnt2 )
        {
            for ( int i = 0 ; i < cnt1 ; i++ )
                if ( c[i] != d[i] ) 
                {
                    flag = false;
                    break;
                }
        }
        else flag = false;
        if ( flag ) puts ( "YES" );
        else puts ( "NO" );
    }       
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值