Test instructions: There are n table legs, to cut off some of the legs so that the rest of the legs can support the table. The maximum number of legs in the remaining table legs can be supported by more than half of the table. It takes a price to cut down every leg of the table. The minimum cost and.
enumeration. If the last remaining table leg is the maximum length of lenth, the length of the table leg has num. Then the length of the table leg is greater than lenth must have been cut off, and then in the rest of the table leg in accordance with the cost from large to small selection of num-1 legs left (not cut), will be the remaining cut off, this must be the minimum cost and. Of course, it is not a brain-free enumeration, It can be enumerated because the price is very small, only 200. First we sort by the length of the table leg, and then enumerate each length as the last remaining length. From small to large enumerations. Because the previous values can be maintained. the cost of using total to record the length of the lenth that needs to be cut is greater than the current enumeration to the length of the table leg, so that from the minimum length of the enumeration, enumeration to a length of lenth can be directly subtracted from all the length of the table leg price lenth, and saved to enumerate the next length. Then save Cnt[i] with the CNT array = length is less than the length of the current enumeration lenth the cost of the total table leg of I. This is why you need to enumerate from small to large. Because the length is greater than lenth, the current enumeration is not logged, and the CNT array is updated after enumerating the current length.
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <string > #include <algorithm> #include <stack> #include <queue> #include <vector> #include <map > #include <set>using namespace std;const int MAX = 100005;const int INF = 100000*200;struct leg{int l; int D;}; int n; Leg Leg[max];int cnt[205]; Number of table legs per cost int total; Total cost bool CMP (Leg L1, Leg L2) {return l1.l < L2.L;} void input () {total = 0; for (int i = 0; i < n; i++) scanf ("%d", &LEG[I].L); for (int i = 0; i < n; i++) {scanf ("%d", &LEG[I].D); Total + = LEG[I].D; }}void solve () {memset (CNT, 0, sizeof (CNT)); Sort (leg, leg + N, CMP); int i = 0, num, temp, cost, ans = INF; while (I < n) {num = 0; Cost = total; for (temp = i; leg[temp].l = = LEG[I].L; temp++) {num++; Cost-= LEG[TEMP].D; } num--; for (int j = 200; J >= 1 && num > 0; j--) {Cost-= min (num, cnt[j]) *j; num-= Cnt[j]; } ans = min (ans, cost); for (temp = i; leg[temp].l = = LEG[I].L; temp++) cnt[leg[temp].d]++; i = temp; } printf ("%d\n", ans);} int main () {while (scanf ("%d", &n)! = EOF) {input (); Solve (); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 557C Arthur and table chop table leg