2 solutions

  • 0
    @ 2022-3-20 21:00:25

    y1s1题目看了很久都没看懂,最后还是看的题解才看懂到底要干嘛

    简单来说就是,有一堆数字,找出数组中数字相同的个数的最大值,相同的数字可以重叠,最后还要计算重叠之后的全部的数字的个数

    #include<bits/stdc++.h>
    using namespace std;
    int n,ans;
    int num[1005];
    int main()
    {
    	std::ios::sync_with_stdio(false);
    	cin>>n;
    	for(int i = 0;i < n; i++)
    		cin>>num[i];
    	sort(num,num+n);
    	int sum = 1,cnt = 1;
    	for(int i = 1; i < n; i++){
    		if(num[i] == num[i-1])
    			cnt++;
    		else{
    			cnt = 1;
    			sum++;
    		}
    		ans=max(ans,cnt);
    	}
    	cout<<ans<<" "<<sum;
    	return 0;
    }
    
    • 0
      @ 2022-3-20 15:16:37

      此题注意一下记录最高长度就行,用到一个快排,当然,说不定可以用桶来装

      #include<iostream>
      #include<algorithm>
      #define INF 0x3f3f3f3f
      using namespace std;
      const int N=1007;
      int n,a[N],b[N],ma=-INF;
      int main(){
      	cin>>n;
      	for(int i=0;i<n;i++)cin>>a[i];
      	sort(a,a+n);
      	b[0]=1;
      	ma=max(b[0],ma);
      	int ans=n;
      	for(int i=1;i<n;i++){
      		if(a[i]==a[i-1]){
      			b[i]=b[i-1]+1;
      			ans--;
      		}
      		else b[i]=1;
      		ma=max(ma,b[i]);
      	}
      	cout<<ma<<" "<<ans;
      	return 0;
      }
      
      • 1

      Information

      ID
      1960
      Time
      2000ms
      Memory
      256MiB
      Difficulty
      5
      Tags
      # Submissions
      32
      Accepted
      15
      Uploaded By