2 条题解

  • 0
    @ 2026-5-18 16:49:54

    思路

    既然想要奶牛数量越多越好,那么就需要尽量得多放奶牛。
    我们可以把奶牛的身高统计起来,不难发现,数量大于等于 22 的身高都可以选其中的两头奶牛。
    最后要注意,身高最高的即使有很多头,也只能选其中的一头。

    code:

    #include<bits/stdc++.h>
    #define int long long
    #define MOD 1000000007
    using namespace std;
    int a[1000005];
    signed main(){
    	cin.tie(0)->sync_with_stdio(0);
    	int T;
    	cin>>T;
    	while(T--){
    		int n,maxn=0;
    		cin>>n;
            for(int i=1;i<=n;i++) a[i]=0;
    		for(int i=1;i<=n;i++){
    			int x;
    			cin>>x;
    			a[x]++;
                maxn=max(maxn,x);
    		}
    		int ans=0;
    		for(int i=1;i<maxn;i++){
    			if(a[i]>=2){
    				ans+=2;
    			}
    		}
            cout<<ans+1<<"\n";
    	}
    	return 0;
    }
    
    • 0
      @ 2025-10-8 16:57:44
      #include<iostream>
      #include<map>
      using namespace std;
      
      map<int,int> m;
      
      int main(){
      	ios::sync_with_stdio(False);
      	cin.tie(0);cout.tie(0);
      	int T,n,t,c,mx;
      	for(cin>>T; T--; cout<<'\n'){
      		cin>>n;
      		m.clear();
      		c = mx = 0;
      		for(int i = 1; i <= n; i++){
      			cin>>t;
      			mx = max(mx,t);
      			m[t]++;
      		}
      		m[mx] = 0;
      		c++;
      		for(auto p : m){
      			if(p.second >= 2){
      				c += 2;
      			}
      		}
      		cout<<c;
      	}
      	return 0;
      }
      
      • 1

      信息

      ID
      1551
      时间
      2000ms
      内存
      256MiB
      难度
      9
      标签
      递交数
      12
      已通过
      6
      上传者