【35.29%】【codeforces 557C】Arthur and Table
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.
Examples
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
【题目链接】:http://codeforces.com/contest/557/problem/C
【题解】
可以从最后的答案出发;
先枚举最大的数是多少;
假设它出现的次数为x;
则为了使得他出现的次数大于总的桌腿个数/2;
则最多还需要增加x-1条桌腿才行;
显然要使得x-1条桌腿耗费的总能量最大;
(因为要消耗的能量是砍掉所有桌腿的能量总和减去这2*x-1条桌腿的能量);
这样消耗的能量最小;
同时,比这个桌腿长的桌腿不能出现,所以只能在比这个桌腿短的桌腿中找x-1条桌腿(总和最大);
具体实现:
用计数排序搞;
一开始按照桌腿长度升序排;
然后顺序枚举桌腿i;
rep1(i,1,n)
{
int l = i,r = i+1,temp=a[i].d;//temp是这2*x-1条桌腿的总能量
while (a[r].l==a[l].l)//是同一种桌腿就累加
{
temp+=a[r].d;
r++;
}
int x = r-l-1;//还要找x-1条桌腿;
//printf("%d\n",x);
rep2(j,200,1)//因为桌腿的能量最大为200所以可以用计数排序,这样满足优先找能量最大的桌腿,且桌腿的长度比该桌腿短.
{
if (x==0) break;//桌腿的数目够了就结束
int s = min(x,bo[j]);
temp+=j*s;//s是这个桌腿的数量,j是它的能量;
x-=s;
}
temp1 = max(temp1,temp);
rep1(j,l,r-1)//搞个前缀和
bo[a[j].d]++;
i = r-1;
}
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define pri(x) printf("%d",x)
#define prl(x) printf("%I64d",x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 1e5+10;
const int MNUM = 200+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
struct abc
{
int l,d;
};
int n,sum=0;
abc a[MAXN];
int bo[MNUM];
bool cmp(abc a,abc b)
{
return a.l < b.l;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
rei(a[i].l);
rep1(i,1,n)
rei(a[i].d),sum+=a[i].d;
sort(a+1,a+1+n,cmp);
int temp1 = 0;
rep1(i,1,n)
{
int l = i,r = i+1,temp=a[i].d;
while (a[r].l==a[l].l)
{
temp+=a[r].d;
r++;
}
int x = r-l-1;
//printf("%d\n",x);
rep2(j,200,1)
{
if (x==0) break;
int s = min(x,bo[j]);
temp+=j*s;
x-=s;
}
temp1 = max(temp1,temp);
rep1(j,l,r-1)
bo[a[j].d]++;
i = r-1;
}
printf("%d\n",sum-temp1);
return 0;
}
【35.29%】【codeforces 557C】Arthur and Table的更多相关文章
- JAVA 基础编程练习题29 【程序 29 求矩阵对角线之和】
29 [程序 29 求矩阵对角线之和] 题目:求一个 3*3 矩阵对角线元素之和 程序分析:利用双重 for 循环控制输入二维数组,再将 a[i][i]累加后输出. package cskaoyan; ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【35.37%】【codeforces 556C】Case of Matryoshkas
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【35.02%】【codeforces 734A】Vladik and flights
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [官方软件] Easy Sysprep v4.3.29.602 【系统封装部署利器】(2016.01.22)--skyfree大神
[官方软件] Easy Sysprep v4.3.29.602 [系统封装部署利器](2016.01.22) Skyfree 发表于 2016-1-22 13:55:55 https://www.it ...
- 【codeforces 29B】Traffic Lights
[题目链接]:http://codeforces.com/problemset/problem/29/B [题意] 一辆车; 让从A开到B; 然后速度是v; (只有在信号灯前面才能停下来..否则其他时 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
随机推荐
- Qt自定义类型使用QHash等算法(Qt已经自定义了34种类型,包括int, QString, QDate等基本数据类型)
自定义类型 #include <QCoreApplication> #include <QSet> #include <QDebug> class testCust ...
- css实现一个缺口小三角
.square{ width:; height:; margin:0 auto; border:6px solid transparent; border-bottom: 6px solid red; ...
- Android 多线程断点续传同时下载多个大文件
最近学习在Android环境中一些网络请求方面的知识,其中有一部分是关于网络下载方面的知识.在这里解析一下自己写的demo,总结一下自己所学的知识.下图为demo的效果图,仿照一些应用下载商城在Lis ...
- element-UI 表单校验失效处理
1.el-form-item 的 prop属性绑定的要是字符串: eg: :prop="'answer[' + 0 + ']' " //而不是 :prop=&qu ...
- 【2017 Multi-University Training Contest - Team 7】Hard challenge
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6127 [Description] 平面上有n个点,每个点有一个价值,每两个点之间都有一条线段,定义 ...
- 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给 ...
- 腾讯2016实习生面试经验(已经拿到offer)
忐忑了好几天,今天最终收到深圳总部的电话.允许录用我为2016年实习生,感觉整个天空都放晴了.坐标:武汉大学,给大家说说我的面试经历吧,我投的是软件开发--应用开发方向. 一.校招流程 投递简历- ...
- Android学习笔记进阶十之Matrix错切变换
刚开始我也不懂啥叫错切变换,一看效果图你就恍然大悟. 对图像的错切变换做个总结: x = x0 + b*y0; y = d*x0 + y0; 与之对应的方法是: Matrix matrix = new ...
- openGLES(二)
顶点和着色器 我们使用独立的点集合构建物体,都是使用顶点,之后会使用着色绘制图性,以及告诉OpenGLES如何绘制的小程序. 片段着色器,即每个小的像素的渲染, 顶点着色器确定所绘制图像的 ...
- .net core 修改网站启动端口
原文:.net core 修改网站启动端口 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yenange/article/details/81675 ...