首页 > 试题广场 >

小度买果汁

[编程题]小度买果汁
  • 热度指数:917 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小度和同事们一起去春游,天气炎热,小度自告奋勇去给大家买果汁。
员工一共有n名(包括小度), 果汁一共有k种,第i位员工每个人都有自己最喜欢的一种果汁a_i
小度到达果汁商店的时候,被告知商店正在举行促销活动,全场果汁买一送一(购买一瓶i型果汁,再赠送一瓶i型果汁)。
小度想让所有人(包括小度)都拿到他们最喜欢的果汁,需要购买多少瓶呢?

输入描述:
第一行两个整数,分别表示员工人数和果汁种类数。
第二行n个整数,表示i号员工最喜欢的果汁种类是a_i


输出描述:
一个整数,表示小度需要购买的瓶数(赠品不算购买的)。
示例1

输入

5 3
1 2 3 1 2

输出

3
示例2

输入

6 2
1 2 1 2 1 2

输出

4
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int m, n, x;
    cin >> m >> n;
    unordered_map<int, int> umap;
    for (int i = 0; i < m; ++i) {
        cin >> x;
        umap[x]++;
    }
    int res = 0;
    for (auto [i, num]:umap) {
        res += (num >> 1) + num % 2;
    }
    cout << res << endl;
    return 0;
}

发表于 2022-02-01 16:50:39 回复(0)
#include<iostream>
#include<map>
using namespace std;

int main(){
    int people,juice;
    int key;
    int ans=0;
    cin>>people>>juice;
    map<int,int> like;
    map<int,int>::iterator it;
    while(cin>>key){
        it=like.find(key);
        if(it!=like.end()){
            it->second++;
        }
        else{
            like.insert(make_pair(key, 1));
        }
    }
    for(it=like.begin();it!=like.end();it++){
        if(it->second%2==0){
            ans=ans+it->second/2;
        }
        else{
            ans=ans+it->second/2+1;
        }
    }
    cout<<ans;
    
}
发表于 2021-09-28 15:04:47 回复(0)
#include <iostream>
#include <vector>
 
usingnamespacestd;
 
int main(){
    int workers, juices;
    cin>>workers>>juices;
    vector<int> likeJuice(workers);
    for(int i=0; i<workers; i++){
        cin>>likeJuice[i];
    }
    vector<int> juiceNums(juices, 0); //vector统计各个种类饮料所需数量
    for(int likeNum:likeJuice){
        juiceNums[likeNum-1]++;
    }
    int cups = 0;
    for(int num:juiceNums){ //偶数杯/2,奇数杯/2+1
        cups+=(num+1)/2;
    }
    cout << cups << endl;
    return 0;
}
发表于 2021-09-06 21:09:13 回复(0)
#include<bits/stdc++.h>
using namespace std;
map<int,int> m;
int main(){
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        m[x]++;
    }
    int res=0;
    for(auto t:m){
        res+=(t.second/2+t.second%2);    
    }    
    cout<<res;
    return 0;

发表于 2023-03-13 21:23:53 回复(0)
#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    
    vector<int> hash(k,0);
    
    for(int i = 0; i < n; ++i)  //统计所有员工喜爱饮料的频次
    {
        int x;
        scanf("%d", &x);
        hash[x - 1]++;
    }
    
    int sum = 0;
    for(auto &x : hash)  //根据频次来计算购买数量
    {
        if(x != 0)
        {
           sum += (x + 1) / 2; 
        }
    }
    
    cout << sum;
    
    return 0;
}

发表于 2022-04-10 15:27:17 回复(0)