CSU-ACM2017暑假集训比赛3D - D CodeForces - 557C

Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable.

In total the table Arthur bought has n legs, the length of the i-th leg is li.

Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number di — the amount of energy that he spends to remove the i-th leg.

A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths.

Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable.

Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the initial number of legs in the table Arthur bought.

The second line of the input contains a sequence of n integers li (1 ≤ li ≤ 105), where li is equal to the length of the i-th leg of the table.

The third line of the input contains a sequence of n integers di (1 ≤ di ≤ 200), where di is the number of energy units that Arthur spends on removing the i-th leg off the table.

Output
Print a single integer — the minimum number of energy units that Arthur needs to spend in order to make the table stable.

Example
Input
2
1 5
3 2
Output
2
Input
3
2 4 4
1 1 1
Output
0
Input
6
2 2 1 1 3 3
4 3 5 5 2 1
Output
8
不得不说这个题暴力的挺巧妙的,首先分析,题目既然要求把凳子变成稳定的最小消耗,稳定是说剩下的凳脚最长的数量要是总数的1/2 以上,不包括1/2,那么暴力的话就是把每种可能的都求解出来,然而,因为消耗体力值的范围很小是1-200,所以先预处理再暴力的话,时间复杂度为O(n *200)。
设给定的凳脚长的集合为{a1,a2,a3,…,an},且a1 < a2 < a3 <..

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 100000000
struct table {
    int lenth;
    int energy;
}a[100005];
int b[100005], cost[205], num[100005];
set<int> c;
bool cmp(table &a, table &b) {
    return a.lenth < b.lenth;
}
int main() {
    int n;
    while (cin >> n) {
        for (int i = 0; i < n; i++) scanf("%d", &a[i].lenth), num[a[i].lenth]++;
        for (int i = 0; i < n; i++) scanf("%d", &a[i].energy);
        sort(a, a + n, cmp);
        int sum = 0;
        for (int i = n - 1; i > 0; i--) {
            sum = sum + a[i].energy;
            if (a[i].lenth != a[i - 1].lenth)
                b[a[i - 1].lenth] = sum;//大于当前的腿长
        }
        int Min = b[a[0].lenth];
        cost[a[0].energy]++;
        for (int i = 1; i < n; i++) {
            if (a[i].lenth != a[i - 1].lenth) {
                int ans = b[a[i].lenth];
                int x = i - num[a[i].lenth] + 1;
                for (int j = 1; j <= 200; j++) {
                    if (cost[j]) {
                        if (cost[j] >= x) {
                            ans = ans + x * j;
                            x = 0;
                            break;
                        }
                        else {
                            ans = ans + cost[j] * j;
                            x = x - cost[j];
                        }
                    }
                }
                Min = min(Min, ans);
            }
            cost[a[i].energy]++;
        }
        cout << Min << endl;
    }
    return 0;
}

你可能感兴趣的:(codeforces,算法)