【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 ...
随机推荐
- VmWare 与 宿主主机通信 STEP BY STEP (适用于刚開始学习的人)
基本原理 在虚拟机中有三种通信方式,例如以下图所看到的 1. Bridged(桥接模式) 在桥接模式下,VMware虚拟出来的操作系统就像是局域网中的一独立的主机,它能够訪问网内不论什么一台机器只是你 ...
- ZOJ Problem Set - 3819Average Score
ZOJ Problem Set - 3819Average Score 题目链接 题目大意:给你两个班的的学生的分数(A,B班).A班有一个学生的分数没有给出. 如今要求你给出这个学生分数的上下限.使 ...
- 洛谷P1316 丢瓶盖
题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? 输入输出 ...
- JS/CSS 在屏幕底部弹出消息
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- 用两个栈实现队列与用两个队列实现栈(Python实现)
用两个栈实现队列: class QueueWithTwoStacks(object): def __init__(self): self._stack1 = [] self._stack2 = [] ...
- 1.html+css页面设计
转自:http://www.cnblogs.com/ywang/archive/2014/04/16/3668638.html 今天用html+css做一个最简单的页面.效果图如下: 说明:这里的韩文 ...
- 9.Spring Boot实战之配置使用Logback进行日志记录
转自:https://blog.csdn.net/meiliangdeng1990/article/details/54300227 Spring Boot实战之配置使用Logback进行日志记录 在 ...
- GO语言学习(十五)Go 语言指针
Go 语言指针 Go 语言中指针是很容易学习的,Go 语言中使用指针可以更简单的执行一些任务. 接下来让我们来一步步学习 Go 语言指针. 我们都知道,变量是一种使用方便的占位符,用于引用计算机内存地 ...
- vb.net 调用api
Public Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) ...
- Tomcat redis 配置
http://www.cnblogs.com/interdrp/p/4868740.html http://blog.csdn.net/qq584852076/article/details/4650 ...