CodeForces 557C Arthur and Table STL的使用
题意:一个桌子有n条腿,每条腿有一定的长度l,和砍下的花费w,现在规定,桌子稳的条件是长度最长的腿(可多个)的数量大于长度小于它的桌子腿数量,且不存在比他还长的桌子腿,求让桌子腿稳定的最小的花费
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const double pi=acos(-);
const int mod=;
int max(int a,int b)
{return a>b?a:b;};
int min(int a,int b)
{return a<b?a:b;}; const int maxl=;
struct node{
int l,w;
}ne[]; bool cmp(node a,node b)
{
return a.l<b.l;
} vector<int> numw[];
int n,num[maxl+],total[maxl+]; void solve()
{
int ans=inf;
for(int i=;i<=maxl;i++)
if(num[i])
{
int temp=total[i+],k=num[i]-num[i+];
int cnt=num[]-num[i]-(k-);
if(cnt>)
{
for(int j=;j<=;j++)
if(numw[j].size())
{
//printf(":2\n");
if(cnt<=) break;
int p=lower_bound(numw[j].begin(),numw[j].end(),i)-numw[j].begin();//花费为j的桌子腿中长度>=i的个数
temp+=min(p,cnt)*j;
cnt-=p;
// printf(":3\n");
}
}
ans=min(ans,temp);
}
printf("%d\n",ans);
} void init()
{
memset(total,,sizeof(total));
memset(num,,sizeof(num));
for(int i=;i<=;i++)
numw[i].clear();
} void input()
{
init();
//printf("||5\n");
for(int i=;i<=n;i++)
{
scanf("%d",&ne[i].l);
num[ne[i].l]++;
}
for(int i=;i<=n;i++)
{
scanf("%d",&ne[i].w);
numw[ne[i].w].push_back(ne[i].l);
}
//printf("|||6\n");
sort(ne+,ne+n+,cmp);
for(int i=;i<=n;)
{
int p=i;
while(ne[p].l==ne[i].l&&i<=n)
{
total[ne[p].l]+=ne[i].w;
i++;
}
}
for(int i=maxl;i>=;i--)
{
total[i]+=total[i+];
num[i]+=num[i+];
}//total数组统计砍下长度>=i的所有桌子腿的总花费
//num数组记录长度>=i的桌子腿的个数
for(int i=;i<=;i++)
if(numw[i].size())
sort(numw[i].begin(),numw[i].end());
} int main()
{
while(~scanf("%d",&n))
{
input();
solve();
}
return ;
}
核心思路:先暴力枚举最长的桌子腿腿长(1e5),然后再按花费(<=200)进行从小到大的贪心选取
,每次在花费为w上进行贪心时,找出小于当前枚举的长度的桌子腿个数,本题的实现是个大难题
错因分析:本来想用优先队列来维护权花费为w的长度序列的,实现本代码中vector的操作,但是优先队列
没有迭代器,不支持begin()这个操作,也就是priority_queue<node> a[100],a[1]就是代表一个优先队列,没有a[1].begin()这个操作,事实上优先队列只有pop(),top(),和insert(),这三个操作,其实就是个堆。
CodeForces 557C Arthur and Table STL的使用的更多相关文章
- Arthur and Table CodeForces - 557C
Arthur and Table CodeForces - 557C 首先,按长度排序. 长度为p的桌腿有a[p]个. 要使得长度为p的桌腿为最长,那么要按照代价从小到大砍掉sum{长度不到p的腿的数 ...
- Codeforces Round #311 (Div. 2) C. Arthur and Table Multiset
C. Arthur and Table Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...
- Codeforces Round #311 (Div. 2)C. Arthur and Table
C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes input standard ...
- C. Arthur and Table(Codeforces Round #311 (Div. 2) 贪心)
C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF-557C Arthur and Table 权值线段树
Arthur and Table 题意 一个桌子有n个腿,每个腿都有一个高度,当且仅当最高的腿的数量大于桌子腿数量的一半时,桌子才是稳定的.特殊的是当只有一个腿时,桌子是稳定的,当有两个腿时两个腿必须 ...
- 【35.29%】【codeforces 557C】Arthur and Table
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 448 D. Multiplication Table
二分法判断答案 D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes inp ...
- Codeforces 448 D. Multiplication Table 二分
题目链接:D. Multiplication Table 题意: 给出N×M的乘法矩阵要你求在这个惩罚矩阵中第k个小的元素(1 ≤ n, m ≤ 5·10^5; 1 ≤ k ≤ n·m). 题解: n ...
- Codeforces 40 E. Number Table
题目链接:http://codeforces.com/problemset/problem/40/E 妙啊... 因为已经确定的格子数目严格小于了$max(n,m)$,所以至少有一行或者一列是空着的, ...
随机推荐
- Hadoop简介及架构
狭义上来说,hadoop就是单独指代hadoop这个软件, 广义上来说,hadoop指代大数据的一个生态圈,包括很多其他的软件 2.hadoop的历史版本介绍 0.x系列版本:hadoop当中最早的一 ...
- VMware 无法开机
无法打开磁盘 虚拟机无法打开磁盘 "或者某一个快照所依赖的磁盘 原因:未能锁定文件"的解决办法 很多人在使用虚拟机是都会遇到“无法打开磁盘" x:\*\*vmdk &qu ...
- sql server lower函数
lower()函数 --将大写字符数据转换为小写字符数据 语法:lower(character_expression) --character_expression是指定要进行转换的字符串
- nodejs---crypto模块MD5签名
1.MD5是一种常用的哈希算法,用于给任意数据一个“签名”.这个签名通常用一个十六进制的字符串表示: /*md5签名*/ /*引入crypto模块*/ const crypto = require(' ...
- ES6基本常见语法
特色:写法更加优雅,更加像面像对象的编程,其思想和 ES5 是一致的. 箭头函数.this ES6中可以使用 => 作为函数表达形式,极简风格,参数+ => +函数体. var foo = ...
- python多线程、多进程、协程笔记
import threading import time import multiprocessing import asyncio movie_list = ['斗破.avi', '复仇者联盟.mp ...
- JS通过sort(),和reverse()正序和倒序
sort()正序 var array1 = [0,1,5,10,15]; array1.sort();//结果为:0,1,10,15,5 请注意,上面的代码没有按照数值的大小对数字进行排序,要 ...
- Linux——Session复制中的失败的可能原因之一
组播地址问题 route add -net 224.0.0.0 netmask 240.0.0.0 dev eno16777728(自己的网卡名)
- eclipse查看jsp出现failed to create the part's controls的解决方法
问题描述 最近eclipse出现了一下问题,某个类无法打开,显示Failed to create the part’s controls ,如图: 问题解决 安装的JDK 版本估计有冲突导致IDE出现 ...
- label smooth
图像分类的一个trick,推导可参考这位博主https://leimao.github.io/blog/Label-Smoothing/ 知乎上的讨论https://www.zhihu.com/que ...