题目链接: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 查各种三角形的个数的更多相关文章

  1. hdu 1396 Counting Triangles(递推)

    Counting Triangles Problem Description Given an equilateral triangle with n thelength of its side, p ...

  2. Counting Triangles(hd1396)

    Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. UVA 12075 - Counting Triangles(容斥原理计数)

    题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...

  4. 1307 - Counting Triangles

    1307 - Counting Triangles    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  5. 统计无向图中三角形的个数,复杂度m*sqrt(m).

    统计无向图中三角形的个数,复杂度m*sqrt(m). #include<stdio.h> #include<vector> #include<set> #inclu ...

  6. LeetCode:有效三角形的个数【611】

    LeetCode:有效三角形的个数[611] 题目描述 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有 ...

  7. Leetcode 611.有效三角形的个数

    有效三角形的个数 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 (使用第一个 ...

  8. LeetCode 611. 有效三角形的个数(Valid Triangle Number)

    611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...

  9. Java实现 LeetCode 611 有效三角形的个数(双指针)

    611. 有效三角形的个数 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 ( ...

随机推荐

  1. linux mysql 查看数据库大小

    SELECT CONCAT(TRUNCATE(SUM(data_length)//,),'MB') AS data_size, CONCAT(TRUNCATE(SUM(max_data_length) ...

  2. 「AHOI2014/JSOI2014」奇怪的计算器

    「AHOI2014/JSOI2014」奇怪的计算器 传送门 我拿到这题首先是懵b的,因为感觉没有任何性质... 后来经过同机房dalao的指导发现可以把所有的 \(X\) 放到一起排序,然后我们可以发 ...

  3. Weka算法算法翻译(部分)

    目录 Weka算法翻译(部分) 1. 属性选择算法(select attributes) 1.1 属性评估方法 1.2 搜索方法 2. 分类算法 2.1 贝叶斯算法 2.2 Functions 2.3 ...

  4. Apache Shiro安全(权限框架)学习笔记二

    课程目标 通过学习本课程掌握权限管理的设计思想及方法,使用Shiro框架完成权限管理功能开发. 1.  理解基于资源的权限管理方法. 2.  掌握权限管理的数据模型. 3.  掌握不使用shiro开发 ...

  5. nginx防盗链处理模块referer和secure_link模块

    使用场景:某网站听过URI引用你的页面:当用户在网站点击url时:http头部会通过referer头部,将该网站当前页面的url带上,告诉服务本次请求是由这个页面发起的 思路:通过referer模块, ...

  6. day1-4js算术运算符及类型转化

    一,JS的运行环境 在html中使用JS,浏览器去解析 NodeJS环境内封装了JS的解析器 二,JavaScript的特点 1.客户端执行 2.执行顺序自上而下 3.弱类型(数据类型)语言 var ...

  7. static在c\c++中的作用(翁恺c++公开课[28-29]学习笔记)

    static相对来说是一个较复杂的修饰符,c++中的static在c的基础之上又包含了static在类中的应用(也就是说多了static的成员变量和static的成员函数):c\c++中静态变量.对象 ...

  8. java 依赖注入

    https://blog.csdn.net/coderder/article/details/51897721 前言 在软件工程领域,依赖注入(Dependency Injection)是用于实现控制 ...

  9. HDU 5570:balls 期望。。。。。。。。。。。。。。。

    balls  Accepts: 19  Submissions: 55  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 65536/655 ...

  10. Linux centosVMware Tomcat介绍、安装jdk、安装Tomcat

    一.Tomcat介绍 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开 ...