2016 CCPC-Final A - The Third Cup is Free

A - The Third Cup is Free
Time limit 6000 ms Memory limit 32768 kB

Description
Panda and his friends were hiking in the forest. They came across a coffee bar inside a giant tree trunk.
Panda decided to treat everyone a cup of coffee and have some rest. Mr. Buck, the bartender greeted Panda and his animal friends with his antler. He proudly told them that his coffee is the best in the forest and this bar is a Michelin-starred bar, thats why the bar is called Starred Bucks.
There was a campaign running at the coffee bar: for every 3 cups of coffee, the cheapest one is FREE. After asking all his friends for their flavors, Panda wondered how much he need to pay.

Input
The first line of the input gives the number of test cases, T.
T test cases follow. Each test case consists of two lines. The first line contains one integer N, the number of cups to be bought.
The second line contains N integers p1,p2,⋅⋅⋅,pNp1,p2,···,pN representing the prices of each cup of coffee.

Output
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the least amount of money Panda need to pay.
∙∙ 1 ≤ T ≤ 100.
∙∙ 1 ≤ N ≤ 105105.
∙∙ 1 ≤ pi ≤ 1000.

Sample Input
2
3
1 2 3
5
10 20 30 20 20

Sample Output
Case #1: 5
Case #2: 80

水题水题,就是每三个里面最便宜的一个可以免费嘛,简单sort一下,当为三的倍数时跳过就好啦 (。・ω・)ノ゙

#include
#include
#include
#include
using namespace std;
int zu;
int n;
int a[100005];
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int k=1;
    scanf("%d",&zu);
    while(zu--)
    {

        scanf("%d",&n);
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a+1,a+n+1,cmp);
        //cout<
        int sum=0;
        for(int i=1;i<=n;i++)
            {
                if(i%3==0)
                {
                    a[i]=0;
                    sum+=a[i];
                }
                else
                    sum+=a[i];
            }
        printf("Case #%d: %d\n",k++,sum);
    }
}

你可能感兴趣的:(sort函数)