首页 > 其他分享 >51 Nod 2497 数三角形 二分

51 Nod 2497 数三角形 二分

时间:2023-04-24 20:02:15浏览次数:32  
标签:Nod ans1 ans2 51 mid 2497 int && include


小b有一个仅包含非负整数的数组a,她想知道有多少个三元组(i,j,k),满足i<j<k且a[i],a[j],a[k]可能作为某个三角形的三条边的边长。


 收起


 



输入



第一行输入一个正整数n,表示数组a中元素个数; 第二行n个非负整数,表示a中元素,以空格隔开; 其中0<n≤1000,a中任意元素a[i]满足0≤a[i]≤1000。



输出



输出一个数,表示满足题意的三元组个数



输入样例

4
2 2 3 4



输出样例

3


很容易想到一个O(n^2 * logn)的解法,我们将原来数组排完序后,枚举两个较小的边,然后二分出最长边的范围即可;



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<map>
using namespace std;
#define maxn 100005
#define rdint(x) scanf("%d",&x)
typedef long long ll;

int n;
int a[1002];
int main() {
	rdint(n);
	for (int i = 1; i <= n; i++)rdint(a[i]);
	int tot = 0;
	sort(a + 1, a + 1 + n);
//	int nn = unique(a + 1, a + 1 + n) - (a + 1);
	for (int i = 1; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			int l = j + 1;
			int r = n;
			int ans1 = -1;
			while (l <= r) {
				int mid = (l + r) / 2;
				if (a[mid] > (a[j] - a[i])) {
					r = mid - 1; ans1 = mid;
				}
				else {
					l = mid + 1;
				}
			}
			ans1 = upper_bound(a + 1 + j + 1, a + 1 + n, a[j] - a[i]) - (a + 1);
			l = j + 1; r = n;
			int ans2 = -1;
			ans2 = lower_bound(a + 1 + j + 1, a + 1 + n, a[i] + a[j]) - (a + 1);
//			cout << ans1 << ' ' << ans2 <<' '<< a[i] + a[j] << endl;
			if (ans1 == -1 || ans2 == -1)continue;
//			tot += (ans2 - ans1 + 1);

			
			else if (ans2 == ans1 && a[ans1] > a[i] + a[j]) {
				tot += 0;
			}
			else if (ans2 == ans1 && a[ans2] < a[i] + a[j]) {
//				cout << 2 << endl;
				tot += (ans2 - ans1 + 1);
			}
			else if (ans2 > ans1 && a[ans2]<a[i]+a[j]) {
				tot += (ans2 - ans1 + 1);
			}
			else if (ans2 > ans1&& a[ans2] >= a[i] + a[j]) {
				tot += (ans2 - ans1);
			}
		}
	}
	printf("%d\n", tot);
//	system("pause");
}



  



标签:Nod,ans1,ans2,51,mid,2497,int,&&,include
From: https://blog.51cto.com/u_15657999/6221823

相关文章

  • Movie collection UVA - 1513
    有n个影碟,标号为1~n,位置为0~n-1,每次取出一个影碟看完后,将其放在最前面(标号为0处),问每个影碟取出前,其位置之前有多少个影碟 开2倍数组,"i放置前面"这个操作add(i,-1),add(newi,1)  #include<iostream>#include<cstring>#include<algorithm>#include<vector>usingn......
  • make_ext4fs error: make_file: failed to allocate inode
    make_ext4fserror:make_file:failedtoallocateinodeerror:do_inode_allocate_extents:Failedtoallocate1146blocksmakemenuconfigTargetImages--->[*]ext4--->(60000)Maximumnumberofinodesinrootfilesystem无脑6000改为60000同样的还有改sys......
  • Innodb实现事务流程
    Innodb通过BufferPool,LogBuffer,RedoLog,UndoLog来实现事务,以update语句为例:Innodb在收到update语句后,会先根据条件找到数据所在⻚,并将该⻚缓存在BufferPool执⾏update语句,修改BufferPool中的数据针对update语句⽣成⼀个RedoLog对象,并存⼊LogBuffer针对update语句......
  • Vue学习笔记之Node Sass version 8.0.0 is incompatible with 4.0.0错误
    输入以下两个命令:npmuninstallnode-sassnpmi-Dsass注:Mac环境如果进行了系统升级,需要重新安装Xcode,执行命令xcode-selectinstall不然会出现如下的错误Mac解决gyp:NoXcodeorCLTversiondetected!报错 如果出现python2的错误gypverb`which`failedE......
  • 记录一次nodejs操作mongodb报错
    记录一次使用Mongoose操作mongodb报错Mongoose查询回调函数报错BookModel.findOne({name:'Rust'},(err,data)=>{if(err){console.log('读取失败');return;}//输出data变量的值console.log(data);......
  • C51笔记-郭天祥-第二章 从点灯大师开始
    第2章  Keil软件的使用及流水灯设计 Keil的用法:用Keil建立工程;            工程配置;            C51单片机程序软件仿真、单步、全速、断点设置和变量查看等; 用一个完整的C51程序操控LED亮灭;调用库函数实现流水灯;蜂鸣器与继电器的操作方法,集......
  • Node.js Event Loop & V8 engine & libuv All In One
    Node.jsEventLoop&V8engine&libuvAllInOne事件循环constcb1=()=>console.log(`1`);constcb2=()=>console.log(`2`);constcb3=()=>console.log(`3`);constcb4=()=>console.log(`4`);constcb5=()=>console.log(......
  • 51单片机 90g360°舵机控制
    主函数main#include<REGX52.H>#include"Delay.h"#include"Timer0.h"unsignedcharcount=0;unsignedcharpwm;voidTimer0()interrupt1{//每隔0.1ms=100us进入TH0=65435/256;//设置定时初始值TL0=6......
  • Node工程的依赖包管理方式
    作者:京东零售陈震在前端工程化中,JavaScript依赖包管理是非常重要的一环。依赖包通常是项目所依赖的第三方库、工具和框架等资源,它们能够帮助我们减少重复开发、提高效率并且确保项目可以正确的运行。目前比较常见的前端包管理器有npm和Yarn,npm是Node.js自带的包管理器,它......
  • 全志v851s 的uart3 设置成普通串口收发
    本文转载自:https://bbs.aw-ol.com/topic/3281/由于UART0被设定为系统dubug输出(简单来说就是将ttyS0设定为console),所以使用UART3作为普通的串口,进行与别的设备通信。1.查看硬件电路图SCH_Schematic1_2022-11-23,查看uart3的TXRXpin脚电路图pdf路径:Yuzukilizard/Hardware/......