codeforces 1A 解题报告
2013-07-04 20:20
387 查看
来自Vic_:http://blog.csdn.net/Vic___
每周一道算法题安慰自己竞赛的心,囧
1A 水题一道
原题地址:http://codeforces.com/problemset/problem/1/A
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
output
rectangular adj.
矩形的;成直角的
shape n.
形状;模型;身材;具体化
pave vt.
铺设;安排;作铺设之用
parallel n.
平行线;对比
看下列图解:
![](http://img.blog.csdn.net/20130704200326140?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVmljX19f/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
左边是n*m的被覆盖矩形,右边是用来覆盖的正方形
很容易想到一种解法,就是沿着每边铺完
![](http://img.blog.csdn.net/20130704200625671?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVmljX19f/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
左边就是覆盖的铺满了,但是难点在于不好确定怎么样去覆盖,怎么确定覆盖位置
所以,用到贪心算法思想,想到一个此时的最优解
贪心算法更多参考:http://en.wikipedia.org/wiki/Greedy_algorithm
每次都沿着上一次铺过的边继续铺
![](http://img.blog.csdn.net/20130704201036265?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVmljX19f/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
左图就是这种铺法,可以得到同样结果
对此建模
![](http://img.blog.csdn.net/20130704201210765?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVmljX19f/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
很容易就能解出算法:
n/a向上取整 * m/a向上取整
![](http://img.blog.csdn.net/20130704201610062?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVmljX19f/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
果断通过
每周一道算法题安慰自己竞赛的心,囧
1A 水题一道
原题地址:http://codeforces.com/problemset/problem/1/A
问题描述
A. Theatre Squaretime 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)设置精度为零
果断通过
粗制滥造,欢迎斧正
相关文章推荐
- 解题报告 之 CodeForces 581C Developing Skills
- CodeForces 778B Bitwise Formula 解题报告
- codeforces B. Coach 解题报告
- codeforces 485A.Factory 解题报告
- CUGBACM Codeforces Tranning 5解题报告
- codeforces 1B 解题报告
- CodeForces 240D D. Merging Two Decks 解题报告 贪心
- codeforces 460C. Present 解题报告
- Google Code Jam Round 1A 2015 解题报告
- CODEFORCES 534A 解题报告
- codeforces 468A. 24 Game 解题报告
- Codeforces 808F 网络流最小割(二分图最大点权独立集) 解题报告
- codeforces 443 B. Kolya and Tandem Repeat 解题报告
- CodeForces 115C. Plumber 解题报告
- 校内赛 codeforces 827D【最小生成树】【树链剖分】 解题报告
- 解题报告 之 CodeForces 581D Three Logos
- [Codeforces]Round246 div2 解题报告
- CODEFORCES 535A 解题报告
- codeforces解题报告与总结
- 2018.1.29【 CodeForces - 455A 】解题报告(dp,long long)