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)$,所以至少有一行或者一列是空着的, ...
随机推荐
- zookeeper知识
zookeeper是一个管理的作用 zookeeper有一个老大叫:leader.跟着老大的有两个小弟follwer,follwer 叫做跟随者 连接zookeeper的六个节点我们称它为客户端 zo ...
- 卸载mysql后再安装提示The service already exists!问题解决方法
卸载mysql后再安装输入mysqld --install 回车后提示The service already exists! 原因:卸载的时候没有卸载干净 方法: 一.重新以管理员身份打开cmd 二. ...
- mybatis 基础(二) 动态sql 关于where if / where choose when otherwise
个人理解: where if就相当于正常的java中的if 语句,如果有多个条件组合判断的话用 and, or连接 而where choose when otherwise choose就好像是swi ...
- CSS3面包屑菜单导航
在线演示 本地下载
- java对象的几种创建过程
java对象的创建过程 (1)用new 语句创建对象,这是最常用的创建对象方法. 下面用一个简单的存在继承关系的实例的创建,来叙述对象创建过程中的细节 概括如下: 执行顺序:(优先级从高到低.)静态代 ...
- linux复习5
权限----------------- r //100 = 4 //文件 :读取内容, //文件夹:是查看文件夹的内容 w //文件 :写数据到文件 //文件夹:增删文件. //10 = 2 x // ...
- docker 入门(1)
1,docker 的安装卸载 https://docs.docker.com/install/linux/docker-ce/ubuntu/ 2,docker中的基本概念 镜像(Image) 容器(C ...
- jq sku实现
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- SpringMVC基础03——常用注解之@RequestMapping
1.用法 SpringMVC使用@RequestMapping注解,为控制器指定可以处理哪些URL请求,并且可以指定处理请求的类型(POST/GET),如果@RequestMapping没有指定请求的 ...
- Maven配置和使用
Eclipse下的Maven开发环境搭建. 现在要用到Eclipse开发Maven的项目,需要配置Maven,简单整理下配置方法. 1.下载Maven安装包,http://maven.apache.o ...