CodeForces - 1250B The Feast and the Bus (贪心+暴力)
题意
https://vjudge.net/problem/CodeForces-1250B
每个人属于队伍ai,汽车一次至多载两只队伍(全员),费用为车的容量*载人次数,问最少花费。
思路
k(队伍数)只有8000,从这个条件入手这题。先对每个队伍按人数从小到大排序,那么a[k]就是车的最小容量,于是我们可以枚举车的容量i从a[k]开始,用l=1和r=k从两端遍历数组a,如果a[l]+a[r]<=i,那么l++,r--;否则让人数大的队伍先上,即r--。但是如果设车的容量为a[k]+a[k-1],那么会因为很恶心的数据TLE。所以要优化枚举的容量,我们贪心的分配队伍上车,一般都会让人数最多的人数最少的一起上,即a[1]+a[k],a[2]+a[k-1],……只要在这些取最大值就是车容量的上限了。
代码
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=5e5+5;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll a[N];
int main()
{
std::ios::sync_with_stdio(false);
ll n,k;
cin>>n>>k;
for(int i=1; i<=n; i++)
{
int x;
cin>>x;
a[x]++;
}
if(k==1)
{
cout<<a[1]<<endl;
return 0;
}
ll mx=0;
for(int i=1;i<=k;i++)
{
mx=max(mx,a[i]+a[k-i+1]);
}
sort(a+1,a+1+k);
ll res=1e16;
for(ll i=a[k]; i<=mx; i++)
{
ll l=1,r=k,cnt=0;
while(l<r)
{
cnt++;
if(a[l]+a[r]<=i)
l++,r--;
else
r--;
}
if(l==r)
cnt++;
res=min(res,i*cnt);
}
cout<<res<<endl;
return 0;
}
CodeForces - 1250B The Feast and the Bus (贪心+暴力)的更多相关文章
- Codeforces 990 调和级数路灯贪心暴力 DFS生成树两子树差调水 GCD树连通块暴力
A 水题 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace ...
- The 10th Shandong Provincial Collegiate Programming Contest H.Tokens on the Segments(贪心+优先级队列 or 贪心+暴力)
传送门 •题意 二维平面上有 n 条线段,每条线段坐标为 $(l_i,i),(r_i,i)$: 平面上的每个整点坐标上都可以放置一枚硬币,但是要求任意两枚硬币的横坐标不相同: 问最多有多少条线段可以放 ...
- Codeforces Round #484 (Div. 2) B. Bus of Characters(STL+贪心)982B
原博主:https://blog.csdn.net/amovement/article/details/80358962 B. Bus of Characters time limit per tes ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- Educational Codeforces Round 7 E. Ants in Leaves 贪心
E. Ants in Leaves 题目连接: http://www.codeforces.com/contest/622/problem/E Description Tree is a connec ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Codeforces Round #180 (Div. 2) B. Sail 贪心
B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...
- codeforces Gym 100187F F - Doomsday 区间覆盖贪心
F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...
随机推荐
- Linux相关集合
本篇概述 Linux xshell6 连接 Hadoop 启动关闭 Linux xshell6 连接相关问题 首先,虚拟机 得先能通成网(具体教程可百度) 然后,进行 本机 ip 的查询(xshell ...
- Azure 架构师认证考试
Azure认证路线 AZ-300 AZ-301 https://blog.csdn.net/pg_edb/article/details/86794505 免费题 https://iteablue.c ...
- XMind 是一个全功能的思维导图和头脑风暴软件,为激发灵感和创意而生
XMind 是一个全功能的思维导图和头脑风暴软件,为激发灵感和创意而生 https://www.xmind.cn/
- GitHub最强技术面试手册:Tech Interview Handbook
摘要: 求职还是需要认真准备的. 原文:超实用技术面试手册,从工作申请.面试考题再到优势谈判,GitHub获30000星 作者:量子位 技术人员求职面试,单刷leetcode上的大厂题库可能还不够. ...
- benchmarks (主要用于分布式文件系统性能测试)
最近进行学习过程中,遇到一些可以用得上的benchmark,在这里进行简要记录:之后估计会用到 TensorFlow benchmarks https://github.com/tensorflow/ ...
- Shell—文件内容操作
读取文件的每一行内容并输出 #!/bin/bash # 方法1 while read line do echo $line done < a.txt # 方法2 cat a.txt | whil ...
- 垃圾收集器GC
(1)DefNew(串行)收集器 Serial(串行)垃圾收集器是最基本.发展历史最悠久的收集器:JDK1.3.1前是HotSpot新生代收集的唯一选择: 特点: (1) 针对新生代采用复制算法,单线 ...
- CF620C Pearls in a Row
CF620C Pearls in a Row 洛谷评测传送门 题目描述 There are nn pearls in a row. Let's enumerate them with integers ...
- phoenix中添加二级索引
Phoenix创建Hbase二级索引 官方文档 1. 配置Hbase支持Phoenix创建二级索引 1. 添加如下配置到Hbase的Hregionserver节点的hbase-site.xml ...
- vscode 笔记
设置中文 查看 --> 命令面板 --> 输入: change display language , 安装 中文, 重启 vscode . markdown 转 pdf 安装 Markdo ...