您的位置:首页 > 其它

codeforces 1A 解题报告

2013-07-04 20:20 387 查看
来自Vic_:http://blog.csdn.net/Vic___

每周一道算法题安慰自己竞赛的心,囧

1A 水题一道

原题地址:http://codeforces.com/problemset/problem/1/A

问题描述

A. Theatre Square

time limit per test
2 seconds

memory limit per test
64 megabytes

input
standard input

output
standard output

Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision
was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a.

What is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the
sides of the Square.

Input

The input contains three positive integer numbers in the first line: n,  m and a (1 ≤  n, m, a ≤ 109).

Output

Write the needed number of flagstones.

Sample test(s)

input
6 6 4


output
4


题目理解

学点英语

Theatre Square 剧院广场

rectangular adj.
矩形的;成直角的
shape n.
形状;模型;身材;具体化

pave vt.
铺设;安排;作铺设之用

parallel n.
平行线;对比

我的理解

题目是要在一个n*m矩形上用a*a的正方形,平行地完全覆盖,允许重叠,但不允许裁剪,求最少正方形数

看下列图解:



左边是n*m的被覆盖矩形,右边是用来覆盖的正方形

很容易想到一种解法,就是沿着每边铺完



左边就是覆盖的铺满了,但是难点在于不好确定怎么样去覆盖,怎么确定覆盖位置
所以,用到贪心算法思想,想到一个此时的最优解
贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的局部最优解。

贪心算法更多参考:http://en.wikipedia.org/wiki/Greedy_algorithm

每次都沿着上一次铺过的边继续铺



左图就是这种铺法,可以得到同样结果
对此建模



很容易就能解出算法:
n/a向上取整 * m/a向上取整

解题代码

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main (){
double n, m ,a ;
cin>>n>>m>>a;
cout<<fixed<<setprecision(0)<<ceil(n/a)*ceil(m/a)<<endl;
return 0;
}


代码解释

第一次提交时候出现精度错误



果断包含iomanip文件
fixed定点格式输出
setprecision(0)设置精度为零


果断通过

粗制滥造,欢迎斧正

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: