Codeforces 789A Anastasia and pebbles(数学,思维题)
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.
She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.
Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type.
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
3 2
2 3 4
3
5 4
3 1 8 9 7
5
In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.
Optimal sequence of actions in the second sample case:
- In the first day Anastasia collects 8 pebbles of the third type.
- In the second day she collects 8 pebbles of the fourth type.
- In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type.
- In the fourth day she collects 7 pebbles of the fifth type.
- In the fifth day she collects 1 pebble of the second type.
题目链接:http://codeforces.com/contest/789/problem/A
思路:开始用暴力直接搜,然后太复杂了,然后WA了,看了下别人的题解,发现好像有个这样的计算公式:
设每种石子的数量分别为x(用循环来做),每个口袋可以装的石子数为k
计算每种石子装满一个口袋需要的天数sum=(k+x-1)/k(容易看出它这样做是为了向下取整);
每次将天数进行累加,最终得到的天数sum=(sum+1)/2(同样是向上取整);
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,k,x;
while(scanf("%d%d",&n,&k)!=EOF)
{
int sum=;
while(n--)
{
scanf("%d",&x);
sum+=(x+k-)/k;
}
printf("%d\n",(sum+)/);
}
return ;
}
Codeforces 789A Anastasia and pebbles(数学,思维题)的更多相关文章
- Codeforces 789A Anastasia and pebbles( 水 )
链接:传送门 题意:这个人每次都去公园捡石子,她有两个口袋,每个口袋最多装 k 个石子,公园有 n 种石子,每种石子 w[i] 个,询问最少几天能将石子全部捡完 思路:排个序,尽量每天都多装,如果 k ...
- PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记
PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...
- 789A Anastasia and pebbles
A. Anastasia and pebbles time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces 718E - Matvey's Birthday(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 首先注意到这个图的特殊性:我们对于所有 \(s_i=s_j\) 的 \((i,j)\) 之间都连了条边,而字符集大小顶多只有 \(8\ ...
- Codeforces 643F - Bears and Juice(思维题)
Codeforces 题目传送门 & 洛谷题目传送门 首先直接暴力枚举显然是不现实的,我们不妨换个角度来处理这个问题,考虑这 \(R_i\) 个瓶子中每一瓶被哪些熊在哪一天喝过. 我们考虑对这 ...
- Codeforces 627E - Orchestra(双向链表,思维题)
Codeforces 题目传送门 & 洛谷题目传送门 下设 \(n,m\) 同阶. 首先有一个傻子都会的暴力做法,枚举矩形的上.下边界 \(l,r\),考虑集合多重集 \(S=\{y|x\in ...
- 51Nod 1003 阶乘后面0的数量(数学,思维题)
1003 阶乘后面0的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 n的阶乘后面有多少个0? 6的阶乘 = 1*2*3*4*5*6 = 720 ...
- Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
A. Vladik and Courtesy time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- Gym 100801D Distribution in Metagonia (数学思维题)
题目:传送门.(需要下载PDF) 题意:t组数据,每组数据给定一个数ni(1 ≤ ni ≤ 10^18),把ni拆成尽可能多的数,要求每个数的素因子只包含2和3,且这些数不能被彼此整除,输出一共能拆成 ...
随机推荐
- 【python】input、int、if-else、注释、while、module(random.randint())语法示例
import random luckyNum=random.randint(2,9) i=1 while i<=3: guessNum=input("请你猜猜我的幸运号码:" ...
- 【java】正则表达式简单示例
public class Test { public static void main(String[] args) { String str="135axy"; String r ...
- 配置程序成为Linux服务
最近写了个程序需要随Linux启动时自动运行起来, 查了一些方法后, 通过配置程序成为系统的服务实现了这个需求, 在此记录一下. 测试程序 #! /bin/sh while [ true ] do e ...
- 前端模块化:RequireJS(转)
前言 前端模块化能解决什么问题? 模块的版本管理 提高可维护性 -- 通过模块化,可以让每个文件职责单一,非常有利于代码的维护 按需加载 -- 提高显示效率 更好的依赖处理 -- 传统的开发模式,如果 ...
- iOS自定义文字高度添加行间距
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } span.s1 { } span.s2 { c ...
- 如何高效撤销Git管理的文件在各种状态下的更改
一.背景 企业中我们一般采用分布式版本管理工具git来进行版本管理,在团队协作的过程中,我们难免会遇到误操作,需要撤销更改的情况,那么我们怎么高效的进行撤销修改呢?对于还未提交到暂存区的代码怎么高效撤 ...
- C++ 头文件系列(ostream)
1. 简介 头文件ostream主要定义了一个输出流类模版basic_ostream,该模版继承自basic_ios模版. 2. basic_ostream模版 2.1 sentry类 与basic_ ...
- [置顶]
xamarin android自定义标题栏(自定义属性、回调事件)
自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...
- SharedPreferences 存List集合,模拟数据库,随时存取
PS:SharedPreferences只要稍微学过一点就会用,他本身通过创建一个Editor对象,来存储提交,而editor可以存的格式为 他里面可以存一个Set<String> Set ...
- ElasticSearch 学习记录之ES如何操作Lucene段
近实时搜索 提交(Commiting)一个新的段到磁盘需要一个 fsync 来确保段被物理性地写入磁盘,这样在断电的时候就不会丢失数据.但是每次提交的一个新的段都fsync 这样操作代价过大.可以使用 ...