因为期末,很久没刷题了,CF一直掉……
这个题其实很简单。。因为做法很容易想到嘛。。
就是枚举max=x时,最大能保留多少价值,不断更新ans,
结果就是所有价值和减去ans就好
由于最大能够保留的长度是199+200,所以当max=x时,算最大能保留多少价值,
也是一个循环算出当前长度比x小的那个桌子角的最大的那几个价值之和保留就行了,
这里写的比较绕。。反正看看代码一下就懂了。。。
维护一个map就行了
比赛的时候,因为好久没刷题了,一直没想清楚,一直到比赛结束前还剩下十分钟才恍然大悟。。
复杂度是(logn+200)*n
不过也没办法啦,要期末嘛
C. Arthur and Table
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 thei-th leg isli.
Arthur decided to make the table stable and remove some legs. For each of them Arthur determined numberdi — the amount of energy that he spends to remove thei-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 with5 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 integersli (1?≤?li?≤?105),
whereli is equal to the length of thei-th leg of the table.
The third line of the input contains a sequence of n integersdi (1?≤?di?≤?200),
wheredi is the number of energy units that Arthur spends on removing thei-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.
Sample test(s)
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
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; map<int,int>num; struct Box { int len,val; }box[100010]; bool cmp(Box one,Box two) { return one.len<two.len; } int main() { int n; cin>>n; for(int i=0;i<n;i++) cin>>box[i].len; for(int i=0;i<n;i++) cin>>box[i].val; sort(box,box+n,cmp); int l=-1,r=-2,ans=-1; for(int i=0;i<n;i++) for(int j=i;j<n;j++) if(box[j+1].len!=box[i].len) { for(int k=l;k<=r;k++) num[box[k].val]++; l=i;r=j; map<int,int>::iterator it=num.end(); int len=j-i,sum=0; while(len>0) { if(it==num.begin()) break; it--; for(int k=0;k<it->second&&len>0;k++,len--) sum+=it->first; } for(int k=l;k<=r;k++) sum+=box[k].val; ans=max(ans,sum); i=j; break; } int sum=0; for(int i=0;i<n;i++) sum+=box[i].val; cout<<sum-ans; }
版权声明:本文为博主原创文章,未经博主允许不得转载。