Codestorm:Counting Triangles 查各种三角形的个数
题目链接:https://www.hackerrank.com/contests/codestorm/challenges/ilia
这周六玩了一天的Codestorm,这个题目是真的很好玩,无奈只做出了四道题,自己太菜,difficult的题目一道题都没出,把moderate的题目拿出来总结一下吧。
给了一些棍子,每根棍子的长度各不相同,然后问这些棍子组成的锐角三角形的个数、直角三角形的个数、钝角三角形的个数。
思路很明显,枚举前面两根木棒的长度,然后二分第三根木棒的长度。复杂度O(n^2logn)。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define maxn 10005 typedef long long ll; ll n;
ll val_2[maxn];
ll val[maxn];
ll ha[maxn]; int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); ll i, j, pos, v, v_sum;
ll cnt1, cnt2, cnt3, pos1, pos2, pos3, pos_sum;
memset(ha, 0, sizeof(ha)); scanf("%lld", &n);
for (i = 1; i <= n; i++)
{
scanf("%lld", &val[i]);
val_2[i] = val[i] * val[i];
ha[val[i]]++;
}
sort(val + 1, val + n + 1);
sort(val_2 + 1, val_2 + n + 1);
cnt1 = 0;
cnt2 = 0;
cnt3 = 0;
for (i = 1; i <= n; i++)
{
for (j = i + 1; j <= n; j++)
{
v = val_2[i] + val_2[j];
pos = lower_bound(val_2 + j + 1, val_2 + n + 1, v) - val_2; if (val_2[pos] == v)
{
cnt2 = cnt2 + ha[val[pos]];
pos3 = pos + ha[val[pos]];
}
else
{
pos3 = pos;
}
pos1 = pos - (j + 1); v_sum = val[i] + val[j];
pos_sum = lower_bound(val + j + 1, val + n + 1, v_sum) - val; pos2 = pos_sum - pos3; if (pos1 >= 0)
cnt1 = cnt1 + pos1;
if (pos2 >= 0)
cnt3 = cnt3 + pos2;
}
}
cout << cnt1 << " " << cnt2 << " " << cnt3 << endl;
//system("pause");
return 0;
}
亮点在后面,题解的思想是,如果对每根棍子按长度排好序之后,用sliding window的思想能够把时间复杂度降到O(n^2)。看了一下这个代码,觉得写得更好,充分利用了前面比较的关系。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define MA(x,y) ((x)>(y)?(x):(y)) const int N = 5002;
int a[N], b[N], n; int input()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i] * a[i];
return 0;
} int sol()
{
long long x = 0, y = 0, z = 0; sort(a + 1, a + n + 1);
sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++)
{
int p = i + 1;
int q = i + 1;
for (int j = i + 1; j <= n; j++)
{
while (p<n && b[i] + b[j] >= b[p + 1])
p++;
q = MA(q, p);
while (q<n && a[i] + a[j]>a[q + 1])
q++;
if (b[i] + b[j] == b[p])
{
x += MA(p - 1 - j, 0);
y++;
z += q - p;
}
else
{
x += MA(p - j, 0);
z += q - p;
}
}
}
cout << x << " " << y << " " << z << endl;
return 0;
} int main()
{
input();
sol();
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Codestorm:Counting Triangles 查各种三角形的个数的更多相关文章
- hdu 1396 Counting Triangles(递推)
Counting Triangles Problem Description Given an equilateral triangle with n thelength of its side, p ...
- Counting Triangles(hd1396)
Counting Triangles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA 12075 - Counting Triangles(容斥原理计数)
题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...
- 1307 - Counting Triangles
1307 - Counting Triangles PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
- 统计无向图中三角形的个数,复杂度m*sqrt(m).
统计无向图中三角形的个数,复杂度m*sqrt(m). #include<stdio.h> #include<vector> #include<set> #inclu ...
- LeetCode:有效三角形的个数【611】
LeetCode:有效三角形的个数[611] 题目描述 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有 ...
- Leetcode 611.有效三角形的个数
有效三角形的个数 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 (使用第一个 ...
- LeetCode 611. 有效三角形的个数(Valid Triangle Number)
611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...
- Java实现 LeetCode 611 有效三角形的个数(双指针)
611. 有效三角形的个数 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 ( ...
随机推荐
- 100w并发产生唯一随机id
#coding=utf-8 import time import base64 import getopt import sys import threading import random impo ...
- Jenkins 定时备份插件 ThinBackup
需求 公司的整个测试环境正式环境打包都是用的同一个Jenkins, 该Jenkins是搭建在内部的一台机器上,之前有一台机器的硬盘出了问题,为了安全起见,我们决定备份 Jenkins 的配置和数据. ...
- Python 基础之文件操作与文件的相关函数
一.文件操作 fp =open("文件名",mode="采用的模式",encoding="使用什么编码集")fp 这个变量接受到open的返 ...
- Python - 模块中的"if __name__ == '__main__':"
1.1 如果导入的模块除了定义函数之外还中有可以执行代码,那么Python解释器在导入这个模块时就会执行这些代码. module1.py: def foo(): print('module 1') f ...
- Java源码-集合-LinkedList
基于JDK1.8.0_191 介绍 LinkedList是以节点来保存数据的,不像数组在创建的时候需要申请一段连续的空间,LinkedList里的数据是可以存放在不同的空间当中,然后以内存地址作为 ...
- MAC97A6检测
- centos6.9下 svn 1.7.10版本 编译安装
svn安装推荐文章: 1. http://blog.51cto.com/myhat/786950 2. https://blog.csdn.net/test1280/article/det ...
- java#临时文件目录
String tmpDir=System.getProperty("java.io.tmpdir");
- CSS3-边框(border-radius、box-shadow、border-image)
CSS3中的边框属性:border-radius.box-shadow.border-image 圆角:border-radius 使用 CSS3 border-radius 属性,你可以给任何元素制 ...
- 【剑指Offer面试编程题】题目1370:数组中出现次数超过一半的数字--九度OJ
题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2 ...