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 题目大意:给出若干个点,保证随意三点不共线.随意选三个点作为三角行,其它点若又在该三角形内,则算是该三角形内部的点.问 ...
随机推荐
- linux 抓包 tcpdump 简单应用
在linuxserver上,常常要定位网络问题,就须要用到抓包. 比如:tcpdump -X -s 0 host 10.17.81.22 and port 9999 -w /home/text.cap ...
- List of content management systems
https://en.wikipedia.org/wiki/List_of_content_management_systems Microsoft ASP.NET Name Platform Sup ...
- SQL Server单表已700w+将普通表转换成分区表
最近项目中,某个表数据量爆发时增长,单表已700w+,读写性能急剧下降,所以考虑加入分区表以解燃眉之急,后续还是要分表分库,当然这是后话.下面简要说一下将普通表转为分区表的步骤. 一.创建文件组 ...
- 46. AngularJS所有版本下载
转自:https://www.cnblogs.com/best/tag/Angular/ 官网下载:https://angularjs.org/ AngularJS所有版本下载:https://cod ...
- 22.允许重复的容器(unordered_multiset)
#include <string> #include <iostream> #include <unordered_set> using namespace std ...
- css常用的阴影
一.box-shadow: 0 2px 15px 0 rgba(0,0,0,.15)!important 二. box-shadow: 0 2px 6px 0 rgba(0,0,0,.4); 三. . ...
- poj2823/hdu3415 - 数据结构 单调队列
poj2823 题目链接 长度为N的数组,求宽度k的滑动窗口在数组上滑动时窗口内的最大值或最小值 如果用单调队列做,求最小值时,队列应该严格递增的.所以插入时,队尾大于等于插入值的元素都应被舍弃,因为 ...
- vmware fusion8 序列号
- jquery中$.get()提交和$.post()提交有区别
jquery中$.get()提交和$.post()提交有区别吗? 相同点:都是异步请求的方式来获取服务端的数据: 异同点: 1.请求方式不同:$.get() 方法使用GET方法来进行异步请求的.$.p ...
- 题解 P2431 【正妹吃月饼】
假如做这道题想着用如下朴实的模拟,那肯定要WA至少4个点. #include <iostream> #include <cstdio> using namespace std; ...