当前位置:网站首页>Cf:e. price maximization [sort + take mod + double pointer + pair]
Cf:e. price maximization [sort + take mod + double pointer + pair]
2022-06-11 18:47:00 【Review of the white speed Dragon King】

analysis
First, the remainder of each number is obtained , The integer part can be removed first
Then sort the remainder
And then the two pointers end to end , If it adds up, it can be greater than or equal to k Words , Then integer part +1, Then both pointers move to the middle
Otherwise , The element that the left pointer currently refers to cannot find a suitable match , So move to the right , Then continue to judge
ac code
for _ in range(int(input())):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
sumA = sum([num // k for num in a])
mod = [num % k for num in a]
mod.sort()
# print(new_a)
bonus = 0
s, e = 0, n - 1
# greedy + double points
while s < e:
# gather
if mod[s] + mod[e] >= k:
bonus += 1
s += 1
e -= 1
# pity, just add s
else:
s += 1
print(sumA + bonus)
summary
This kind of pairing problem can consider sorting + greedy + Double pointer
边栏推荐
- uni-app 慕客热搜项目实战(一)tabBar的制作
- Cool visualization tool: first introduction to processing
- cf:C. Restoring the Duration of Tasks【找规律】
- SAP BTP 上 workflow 和 Business Service 的 project 管理
- 疫情下远程办公沟通心得|社区征文
- Visual slam lecture notes-10-1
- Quanzhi technology T3 development board (4-core arm cortex-a7) - video development case
- 力扣刷题——二叉树的层序遍历
- Feign shares login information for request
- 牛客刷题——合法括号序列判断
猜你喜欢

* Jetpack 笔记 Room 的使用

Visual slam lecture notes-10-1

手把手教你学会FIRST集和FOLLOW集!!!!吐血收藏!!保姆级讲解!!!

「案例分享」基于 AM57x+ Artix-7 FPGA开发板——PRU开发手册详解

非递归实现二叉树的前、中、后序遍历

The 2023 MBA (Part-time) of Beijing University of Posts and telecommunications has been launched
Téléchargement et téléchargement des fichiers nécessaires au développement

Introduction to basic use and pit closure of BigDecimal

力扣刷题——根据二叉树创建字符串

In 2023, the MPAcc of School of management of Xi'an Jiaotong University approved the interview online in advance
随机推荐
cf:F. Shifting String【字符串按指定顺序重排 + 分组成环(切割联通分量) + 各组最小相同移动周期 + 最小公倍数】
Project management of workflow and business service on SAP BTP
* Jetpack 笔记 Room 的使用
全国院校MBA、EMBA、MPA、MEM、提前面试(预面试)时间批次已出(持续更新中)-文都管联院
对‘g2o::VertexSE3::VertexSE3()’未定义的引用
Signal processing and capture
Niu Ke's question -- finding the least common multiple
动态爆炸效果
炫酷的可视化工具:processing 初识
Startup process of datanode
Gmail:如何撤回发出的邮件?
Two methods for matlab to save imshow drawing pictures to a specified folder
Summary of common mysql/redis interview questions
如何在 SAP BTP 上 手动执行 workflow
Monitoring loss functions using visdom
牛客刷题——不要二
Flink CDC 在大健云仓的实践
Make a static tank
全志T3开发板(4核ARM Cortex-A7)——系统启动阶段LOGO显示详解
「案例分享」基于 AM57x+ Artix-7 FPGA开发板——PRU开发手册详解