D - Finding lines

本文探讨了一个有趣的问题:如何计算收集公园中不同类型的鹅卵石所需的最少天数,考虑到每次可以携带的鹅卵石数量及类型限制。通过分析每种颜色鹅卵石的数量,并使用特定算法,我们找到了一种高效解决该问题的方法。
Description
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.
Input
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.
Output
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
Sample Input
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output

5

题意:

有n种不同颜色pe,而且每个颜色都有一定的数目,每天只能拿两个能装k个相同颜色pe的包(每个包内的pe为相同颜色)问最少需要几天!

思路:

先算出来总共需要的钱包,注意其中小于k的一种颜色呢pe一定单独占一个包,然后在对大于k的进行计算,计算出总得sum,若sum能被2整除,则输出sum/2否则sum/2+1;

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k,i,sum=0;
	int a,b;
	cin>>n>>k;
	for(i=0;i<n;i++)
	{
		cin>>a;
		if(a<=k)
			sum++;
		if(a>k)
		{
			b=a/k;
			if(a%k==0)
				sum+=b;
			else
				sum+=(b+1);
		}
		
	}
	b=sum/2;
	if(sum%2==0)
		cout<<b<<endl;
	else 
		cout<<b+1<<endl;
	
	
	return 0;
}
心得:

注意寻找解决问题最简便的方法!!!


File: dir Node: Top This is the top of the INFO tree This (the Directory node) gives a menu of major topics. Typing "q" exits, "?" lists all Info commands, "d" returns here, "h" gives a primer for first-timers, "mEmacs<Return>" visits the Emacs topic, etc. In Emacs, you can click mouse button 2 on a menu item or cross reference to select it. * Menu: Archiving * Cpio: (cpio). Copy-in-copy-out archiver to tape or disk. * Tar: (tar). Making tape (or disk) archives. Basics * Common options: (coreutils)Common options. * Coreutils: (coreutils). Core GNU (file, text, shell) utilities. * Date input formats: (coreutils)Date input formats. * File permissions: (coreutils)File permissions. Access modes. * Finding files: (find). Operating on files matching certain criteria. * ed: (ed). The GNU Line Editor. C++ libraries * autosprintf: (autosprintf). Support for printf format strings in C++. Compression * Gzip: (gzip). General (de)compression of files (lzw). Development * libext2fs: (libext2fs). The EXT2FS library. * libffi: (libffi). Portable foreign-function interface library. Editors * nano: (nano). Small and friendly text editor. Emacs * IDN Library: (libidn)Emacs API. Emacs API for IDN functions. GNU C library functions and macros * ALTWERASE: (libc)Local Modes. * ARGP_ERR_UNKNOWN: (libc)Argp Parser Functions. * ARG_MAX: (libc)General Limits. * BC_BASE_MAX: (libc)Utility Limits. * BC_DIM_MAX: (libc)Utility Limits. * BC_SCALE_MAX: (libc)Utility Limits. * BC_STRING_MAX: (libc)Utility Limits. * BRKINT: (libc)Input Modes. * BUFSIZ: (libc)Controlling Buffering. -----Info: (dir)Top, 2017 lines --Top------------------------------------------------------------------------------------------ No menu item `breakpoints' in node `(dir)Top'.
最新发布
09-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值