UVA 12508 - Triangles in the Grid(计数问题)
12508 - Triangles in the Grid
题意:给定一个n∗m格子的矩阵,然后给定A,B。问能找到几个面积在A到B之间的三角形。
思路:枚举每一个子矩阵,然后求[0,A]的个数减去[0,B]的个数就是答案,然后对于每一个子矩阵个数非常好求为(n−r+1)∗(m−c+1)。
关键在于怎么求每一个子矩阵的符合个数。
想了好久,參考别人题解才想出来。分3种情况讨论:
1、一个点在矩形顶点。另外两点相应在顶点的另外两边上。
2、两个点在顶点上。另外一点在对边上。
3、三个点都在顶点上
然后分别去计算求和。详细的过程比較麻烦,在纸上多画画一边就能得到一个o(N)的方法。大概是枚举一个在竖直边上的位置,横的位置利用公式运算一步求解,这样时间复杂度是能够接受的
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int t;
long long n, m, a, b;
long long solve(long long limit) {
long long ans = 0;
for (long long r = 1; r <= n; r++) {
for (long long c = 1; c <= m; c++) {
long long count = 0;
long long up, down;
if (r * c <= limit) count += 2 * (r - 1 + c - 1);
for (long long x = 0; x <= r; x++) {
up = min(c, (x * c + limit) / r);
long long tmp = x * c - limit;
if (tmp <= 0) down = 0;
else down = (tmp - 1) / r + 1;
if (down <= up) count += 2 * (up - down + 1);
}
for (long long x = 1; x < r; x++) {
long long s = (r * c - x);
if (s <= limit) count += 4 * (c - 1);
else count += 4 * ((c - 1) - min((s - limit) / x + ((s - limit) % x != 0), c - 1));
}
ans += count * (n - r + 1) * (m - c + 1);
}
}
return ans;
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%lld%lld%lld%lld", &n, &m, &a, &b);
a <<= 1; b <<= 1; if (a == 0) a = 1;
printf("%lld\n", solve(b) - solve(a - 1));
}
return 0;
}
UVA 12508 - Triangles in the Grid(计数问题)的更多相关文章
- uva 12508 - Triangles in the Grid(几何+计数)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/35244875 题目链接:uva 12508 ...
- UVA 12651 Triangles
You will be given N points on a circle. You must write a program to determine how many distinctequil ...
- UVA 12075 - Counting Triangles(容斥原理计数)
题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...
- uva 10671 - Grid Speed(dp)
题目链接:uva 10671 - Grid Speed 题目大意:给出N,表示在一个N*N的网格中,每段路长L,如今给出h,v的限制速度,以及起始位置sx,sy,终止位置ex,ey,时间范围st,et ...
- Cheerleaders UVA - 11806 计数问题
In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...
- uva 11605 - Lights inside a 3d Grid(概率)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=2652" style=""& ...
- UVA 12382 Grid of Lamps 贪心
题目链接: C - Grid of Lamps Time Limit:1000MSMemory Limit: 0KB 问题描述 We have a grid of lamps. Some of the ...
- UVA 11916 Emoogle Grid(同余模)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 11529 - Strange Tax Calculation(计数问题)
题目链接:uva 11529 - Strange Tax Calculation 题目大意:给出若干个点,保证随意三点不共线.随意选三个点作为三角行,其它点若又在该三角形内,则算是该三角形内部的点.问 ...
随机推荐
- legend---八、php对象如何转换成js对象
legend---八.php对象如何转换成js对象 一.总结 一句话总结:a.直接转换:b.通过json对象做中间桥梁 1.为什么传递给父亲构造函数的参数不能写默认值? 这里的第三行的比如$type不 ...
- zzulioj--1828-- 贪心的小猫咪(贪心模拟)
1828: 贪心的小猫咪 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 14 Solved: 4 SubmitStatusWeb Board Des ...
- windows下git的安装和使用
git到底是个什么东西,我这里就不介绍了,如果大家还有不懂的,可以去百度一下的.我这里给一个介绍的网址:git简介 这里在留一个地址http://baike.baidu.com/subv ...
- Python dataframe中如何使y列按x列进行统计?
如图:busy=0 or 1,求出busy=1时los的平均,同样对busy=0时也求出los的平均 Python dataframe中如何使y列按x列进行统计? >> python这个答 ...
- 数据库Tsql语句创建--约束--插入数据
1.创建数据库 use master go if exists(select * from sysdatabases where name='数据库名字') drop database 数据库名字 g ...
- $.widget 编写jQueryUI插件(widget)
转自:MainTao: 编写jQueryUI插件(widget) 使用jQueryUI的widget来写插件,相比于基本的jquery插件有一些好处: * 方便实现继承,代码重用 * 默认是单例 * ...
- UVa 1600 Patrol Robot【BFS】
题意:给出一个n*m的矩阵,1代表墙,0代表空地,不能连续k次穿过墙,求从起点到达终点的最短路的长度 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰到墙,当前的k减去1,碰到0, ...
- UVa 10305 Ordering Tasks【拓扑排序】
题意:给出n件事情,m个二元组关系,求它们的拓扑序列 用的队列来做 #include<iostream> #include<cstdio> #include<cstrin ...
- java反射与多态(父类调用子类)的代码演示
package Test0817; import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method ...
- phpStudy出现You don't have permission to access / on this server.
原本用的 php 是<5.5.38版本的>,但是项目最低要求是<5.6>,所以就选择切换了版本,但是用原来的域名访问一直出现:You don't have permission ...