zoukankan      html  css  js  c++  java
  • CodeForces 12C Fruits

    Fruits
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times.

    When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).

    Input

    The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.

    Output

    Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.

    Sample Input

    Input
    5 3
    4 2 1 10 5
    apple
    orange
    mango
    Output
    7 19
    Input
    6 5
    3 5 1 6 8 1
    peach
    grapefruit
    banana
    orange
    orange
    Output
    11 30
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     int n,m;
     9     int i,j,k;
    10     int price[105],namss[105];
    11     char nam[105][36];
    12     while(scanf("%d %d",&n,&m)!=EOF)
    13     {
    14         memset(namss,0,sizeof(namss));
    15         for(i=1;i<=n;i++)
    16             scanf("%d",&price[i]);
    17         sort(price+1,price+n+1);
    18         for(i=1;i<=m;i++)
    19         {
    20             k=0;
    21             scanf("%s",&nam[i]);
    22             for(j=1;j<i;j++)
    23             {
    24                 if(strcmp(nam[i],nam[j])==0)
    25                 {
    26                     namss[j]++;
    27                     k=1;i--;m--;
    28                     break;
    29                 }
    30             }
    31             if(k==0)
    32             {
    33                 namss[i]++;
    34             }
    35         }
    36         sort(namss+1,namss+m+1);
    37         /*for(i=1;i<=n;i++)
    38             printf("%d ",price[i]);
    39         printf("
    ");
    40         for(i=1;i<=m;i++)
    41             printf("%d ",namss[i]);
    42         printf("
    ");*/
    43         int ans1=0,ans2=0;
    44         for(i=m;i>=1;i--)
    45         {
    46             ans1=ans1+namss[i]*price[m-i+1];
    47             ans2=ans2+namss[i]*price[n-m+i];
    48         }
    49         printf("%d %d
    ",ans1,ans2);
    50     }
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    cb快捷键
    N的阶乘的长度 V2(斯特林近似)
    最大子序列和(Max Sum ,Super Jumping! Jumping! Jumping! )
    关于莫比乌斯和莫比乌斯反演
    最少拦截系统
    set用法详解
    几种数学公式(环排列 母函数 唯一分解定理 卡特兰数 默慈金数 贝尔数 那罗延数)
    最小堆算法
    并查集算法
    dijkstra算法演示
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771500.html
Copyright © 2011-2022 走看看