The 15th Zhejiang Provincial Competition B King of Karaoke (for negative numbers to become array subscripts)

Topic link: http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5753

The meaning of the question: Given two sequences Dn and Sn, you can add an arbitrary number K to each value in the sequence Dn, and ask the Dn sequence plus K at most how many numbers are equal to the number corresponding to the Sn sequence.

Idea: Subtract 2 sequences directly, but there will be negative numbers, so use

1) map to construct map<int, int> At this time, negative numbers can be stored

2) To calculate the number of occurrences of a number in an array, you can sort first and then use the idea of ​​DP to calculate

! ! ! At the beginning, I misread the title. After reading Dn plus any number, how many numbers are the same in the sequence of Dn and Sn, which forcibly increases the difficulty QAQ

1) map construction version

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=1e5+100;

intmain()
{
    int n,t;
    int a[maxn],b[maxn],c[maxn];
    cin>>t;
    while(t--)
    {
        map<int,int> p;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        for(int i=0;i<n;i++){
            cin>>b[i];
            c[i]=a[i]-b[i];
            p[c[i]]++;
        }
        int ans=1;
        for(int i=0;i<n;i++){
            ans=max(p[c[i]],ans);
        }
        cout<<ans<<endl;
    }
    return 0;
}

2) dp construction version

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=1e5+100;

intmain()
{
    int n,t;
    int a[maxn],b[maxn],c[maxn];
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        for(int i=0;i<n;i++){
            cin>>b[i];
            c[i]=a[i]-b[i];
        }
        sort(c,c+n);
        int ans=1,num=c[0],cnt=1;
        for(int i=1;i<n;i++){
           if(c[i]==num)
            cnt++;
           else
           {
              ans=max(ans,cnt);
              cnt=1;
              num=c[i];
           }
        }
        ans=max(ans,cnt);
        cout<<ans<<endl;
    }
    return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325681938&siteId=291194637