题意

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 (贪心+暴力)的更多相关文章

  1. Codeforces 990 调和级数路灯贪心暴力 DFS生成树两子树差调水 GCD树连通块暴力

    A 水题 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace ...

  2. The 10th Shandong Provincial Collegiate Programming Contest H.Tokens on the Segments(贪心+优先级队列 or 贪心+暴力)

    传送门 •题意 二维平面上有 n 条线段,每条线段坐标为 $(l_i,i),(r_i,i)$: 平面上的每个整点坐标上都可以放置一枚硬币,但是要求任意两枚硬币的横坐标不相同: 问最多有多少条线段可以放 ...

  3. 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 ...

  4. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  5. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  6. 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 ...

  7. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  8. Codeforces Round #180 (Div. 2) B. Sail 贪心

    B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...

  9. codeforces Gym 100187F F - Doomsday 区间覆盖贪心

    F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...

随机推荐

  1. LeetCode刷题191118

    博主渣渣一枚,刷刷leetcode给自己瞅瞅,大神们由更好方法还望不吝赐教.题目及解法来自于力扣(LeetCode),传送门. 算法: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按 ...

  2. oracle表空间相关统计查询

    部分转自 https://www.cnblogs.com/xwdreamer/p/3511047.html--查询表空间使用情况SELECT UPPER(F.TABLESPACE_NAME) &quo ...

  3. MySQL 锁的监控及处理

    故障模拟 # 添加两项配置 vi /etc/my.cnf [mysqld] autocommit=0 innodb_lock_wait_timeout = 3600 systemctl restart ...

  4. HTML Rendering Error

    刚下载的markdown弹窗提示html渲染错误 去官网   http://markdownpad.com/faq.html#livepreview-directx  页面搜索 This view h ...

  5. Python-1-Day

    C = float(input("Enter a degree in Celsius:"))F = (9/5) * C + 32print("{0} Celsius is ...

  6. 201871010123-吴丽丽《面向对象程序设计(Java)》第一周学习总结

                                                                            201871010123-吴丽丽<面向对象程序设计 ...

  7. Spring-AOP-配置实现五大通知

    码云: xml配置方法:https://gitee.com/MarkPolaris/spring_aop_1 注解配置方法:https://gitee.com/MarkPolaris/spring-e ...

  8. springboot整合shiro进行权限管理

    背景:springboot2.1,shiro1.4:由于目前的小项目没做登录,但是客户又需要加上权限,因此楼主就想到了shiro(这是单独的项目,需要集成后台管理系统) shiro简介 Apache ...

  9. Python模块import本质是什么?import是什么

    ​ 写这篇文章主要是对Python的import模块或包的机制有了更深层级的一个理解,也在具体工作中得到了一点实践,这种思考是由上一篇文章<__main__内置模块预加载Shotgun接口的妙用 ...

  10. Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'infor

    今天在Navicat上执行SQL增删改查数据操作的时候出现了下面这个问题 Expression #1 of ORDER BY clause is not in GROUP BY clause and ...