2016 Multi-University Training Contest 2 - 1005 (hdu5738)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738
题目大意:给定平面上的n个点,一个集合合法当且仅当集合中存在一对点u,v,对于集合中任意点w,均有dis(u,v)≥[dis(u,v)+dis(u,w)+dis(v,w)]/2。其中dis(A,B)为A,B两点的欧几里得距离。问你这样的合法集合有多少个。数据范围:1≤n≤1000。
解题思路:对于所给条件简单分析一下就能转化题意,实际上是在求有多少个集合满足集合内的点都在同一直线上。
比赛时的思路一直是,由于可能有重点,于是把重点整合到一个集合里,记一下集合里的个数cnt,那么对于这样一个重点的集合,从中任选至少两个都能作为合法点集,因此对答案的贡献是(2^cnt - cnt - 1)。下面考虑非重点,枚举重点集,对其他重点集按到此点集的距离从小到大排序,再依次枚举,用map记录一下斜率,就能在枚举过程中得到两个重点集之间的点个数K(也就是有多少个点也是这个斜率),对答案的贡献就是(2^cnt1 - 1)*(2^cnt2 - 1)*(2^K)。这样做思路应该没什么问题,只是常数比较大,代码难度也不小。一直到比赛结束也一直WA。
后来看了题解,思路比较清楚。对于这种数方案,要不重不漏的题目要记得使用有序化思想。而在二维坐标中,很常用的两种排序就是x,y双关键字排序,还有极角排序。
首先我们对这n个点做双关键字排序,然后对于x相同的点,个数为cnt,那么对答案的贡献就是(2^cnt - cnt - 1),然后对于每个重点集,对它右面的点做极角排序,做这个排序的意义在于以下就把同一斜率的点搞到了一起,方便计算了。假设当前极角上有p个点那么对答案的贡献就是(2^cnt - 1)*(2^p - 1)。
值得注意的也是十分重要的,就是极角排序的正确做法。首先计算dx, dy,并除掉abs(gcd(dx,dy)),一定要取绝对值!!不然会WA得不明所以!!然后cmp()的时候不要把斜率化成小数比较,直接dyA*dxB<dxA*dyB这样比较可避免精度问题。
总之这道题思想难度不是很大,但是需要的技巧比较多,细节也比较多,是值得一写的题目。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL; const int MaxN = , Pt = 1e9 + ;
struct Point {
int x, y, dx, dy;
}a[MaxN + ], b[MaxN + ];
int T, n;
LL Pow[MaxN + ];
LL ans; int Gcd(int x, int y)
{
if (y == ) return x;
return Gcd(y, x % y);
} void Init()
{
scanf("%d", &n);
Pow[] = ;
for (int i = ; i <= n; i++) {
scanf("%d%d", &a[i].x, &a[i].y);
Pow[i] = (Pow[i - ] * ) % Pt;
}
} bool cmp(Point A, Point B) {
if (A.x == B.x) return A.y < B.y;
return A.x < B.x;
} bool cmp2(Point A, Point B) {
return (LL)A.dy * B.dx < (LL)A.dx * B.dy;
} void Solve()
{
ans = ;
sort(a + , a + n + , cmp);
int L = , R = ;
while (L <= n) {
while (R < n && a[R + ].x == a[R].x) R++;
//printf("%d %d\n", L, R);
ans = (ans + Pow[R - L + ] - - (R - L + )) % Pt;
int l = L, r = L;
while (l <= R) {
while (r < R && a[r + ].y == a[r].y) r++;
//printf("**%d %d\n", l, r);
int tot = ;
for (int i = R + ; i <= n; i++) {
b[++tot].dx = a[i].x - a[l].x;
b[tot].dy = a[i].y - a[l].y;
int D = Gcd(b[tot].dx, b[tot].dy);
if (D < ) D = -D;
b[tot].dx /= D; b[tot].dy /= D;
}
sort(b + , b + tot + , cmp2);
int cnt = ;
for (int i = ; i <= tot; i++) {
if (i == tot || cmp2(b[i], b[i + ])) {
ans = (ans + (Pow[r - l + ] - ) * (Pow[cnt] - )) % Pt;
cnt = ;
}else cnt++;
}
l = r + ; r = r + ;
}
L = R + ; R = R + ;
}
printf("%I64d\n", ans);
} int main()
{
scanf("%d", &T);
for (int i = ; i <= T; i++) {
Init();
Solve();
}
}
2016 Multi-University Training Contest 2 - 1005 (hdu5738)的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- hdu 4939 2014 Multi-University Training Contest 7 1005
Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- 2016 Multi-University Training Contest 2 - 1005 Eureka
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题目大意:给定平面上的n个点,一个集合合法当且仅当集合中存在一对点u,v,对于集合中任意点w,均 ...
- 2016 Multi-University Training Contest 4 - 1005 (hdu5768)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意:给你区间[L,R],问你[L, R]中有多少个数字x满足x%7=0且x%p[i]≠a[ ...
- 2016 Al-Baath University Training Camp Contest-1 E
Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 2016 Al-Baath University Training Camp Contest-1 J
Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...
- 2016 Al-Baath University Training Camp Contest-1 I
Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...
- 2016 Al-Baath University Training Camp Contest-1 H
Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...
随机推荐
- php返回数据格式
PHP返回HTML代码: header('Content-type:text/html; charset=utf-8'); PHP返回xml代码:header('content-type: t ...
- java中Excel导出
转载:https://www.cnblogs.com/gudongcheng/p/8268909.html,稍加修改了 https://www.cnblogs.com/hanfeihanfei/p/7 ...
- RequestContextHolder与RequestContextUtils
org.springframework.web.servlet.support.RequestContextUtils 在spring-webmvc中, 主要用来获取WebApplicationCon ...
- spring boot mysql 事务
mysql默认 事务自动提交.即:每条insert/update/delete语句,不需要程序手工提交事务,而是mysql自行提交了. 如果我们想实现程序事务提交,需要事先关闭mysql的自动提交事务 ...
- Why do you need a new Launch X431 scan tool?
1- 2017 Launch x431 v supports “Special Functions” The 2017 version of Launch x431 v diagnostic tool ...
- 进入wordpress中的模板文件
Wordpress的页面结构 一个简单的wordpress由 头部.内容.页脚组成,每个块中的每一个都由当前wordpress主题中模板文件生成. eg: 头:包含重要的信息,一般都是网页的头部. 内 ...
- Thread 1 cannot allocate new log, sequence 187398
报错信息: Thread 1 cannot allocate new log, sequence 187398Checkpoint not complete 处理方法: 查看REDO日志组 selec ...
- STM32中管脚利用
如果利用4线SWD则剩余的调试引脚可以作为IO使用: void JTAG_Set(unsigned char Mode){ u32 temp; temp=Mode; temp<<=25; ...
- matlab 图像和 opencv 图像的相互转换
matlab可以生成C++代码, 但是在涉及图像数据的时候,要注意数据格式的转换. 1. Matlab图像数据在内存中的存放顺序是R通道图,G通道图,B通道图.对于每个通道,数据存放是先列后行. 2. ...
- 输入一个正整数n (1<n<=10),生成 1个 n*n 方阵 求出对角线之和
#define _CRT_SECURE_NO_WARNINGS #include <Windows.h> #include <stdio.h> #include <std ...