Codeforces Round #587 (Div. 3) B. Shooting(贪心)

链接:

https://codeforces.com/contest/1216/problem/B

题意:

Recently Vasya decided to improve his pistol shooting skills. Today his coach offered him the following exercise. He placed n cans in a row on a table. Cans are numbered from left to right from 1 to n. Vasya has to knock down each can exactly once to finish the exercise. He is allowed to choose the order in which he will knock the cans down.

Vasya knows that the durability of the i-th can is ai. It means that if Vasya has already knocked x cans down and is now about to start shooting the i-th one, he will need (ai⋅x+1) shots to knock it down. You can assume that if Vasya starts shooting the i-th can, he will be shooting it until he knocks it down.

Your task is to choose such an order of shooting so that the number of shots required to knock each of the n given cans down exactly once is minimum possible.

思路:

贪心, 大的优先

代码:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e4+10;

struct Node
{
    int a, id;
    bool operator < (const Node& that) const
    {
        return this->a > that.a;
    }
}node[MAXN];
int n;

int main()
{
    cin >> n;
    for (int i = 1;i <= n;i++)
        cin >> node[i].a, node[i].id = i;
    sort(node+1, node+1+n);
    int cnt = 0, sum = 0;
    for (int i = 1;i <= n;i++)
    {
        sum += node[i].a*cnt+1;
        cnt++;
    }
    cout << sum << endl;
    for (int i = 1;i <= n;i++)
        cout << node[i].id << ' ' ;
    cout << endl;

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/11622772.html