首页 > 其他分享 >[Codeforces] CF1703F Yet Another Problem About Pairs Satisfying an Inequality

[Codeforces] CF1703F Yet Another Problem About Pairs Satisfying an Inequality

时间:2023-11-24 21:11:46浏览次数:32  
标签:10 About Satisfying int CF1703F 测试数据 leq long 整数

时间限制 \(2s\) | 空间限制 \(250M\)

题目描述

给你一个序列$ a_1, a_2, \dots a_n $ 。请计算出满足下面条件的 $(i,j) (1 \leq i, j \leq n) $个数 。

  • $ a_i < i < a_j < j $ .

输入格式

第一行包含一个整数 $ t $ ( $ 1 \leq t \leq 1000 $ ) — 测试数据的个数

每一个测试数据的第一行包含一个整数 $ n $ ( $ 2 \leq n \leq 2 \cdot 10^5 $ ) — 序列的长度

每一个测试数据的第二行包含$ n $ 个整数 $ a_1, a_2, \dots, a_n $ ( $ 0 \leq a_i \leq 10^9 $ ) — 序列里的元素

保证每一个测试数据的$ n $ 的总和不超过 $ 2 \cdot 10^5 $ .

输出格式

对于每一个测试数据,输出一个整数——满足题目条件的\((i,j)\)个数

请注意,对于某些测试数据,他的答案可能会超过32位的整数,所以你应该使用64位的整数在你的编程语言里,比如说C++中的long long

样例输入 #1

5
8
1 1 2 3 8 2 1 4
2
1 2
10
0 2 1 6 3 4 1 2 8 3
2
1 1000000000
3
0 1000000000 2

样例输出 #1

3
0
10
0
1

提示

对于第一组测试数据,满足条件的 $ (i, j) $ = $ {(2, 4), (2, 8), (3, 8)} $ .

  • $ (2, 4) $ 满足条件是因为 $ a_2 = 1 $ , $ a_4 = 3 $ 且$ 1 < 2 < 3 < 4 $ .
  • $ (2, 8) $ 满足条件是因为$ a_2 = 1 $ , $ a_8 = 4 $ 且 $ 1 < 2 < 4 < 8 $ .
  • $ (3, 8) $ 满足条件是因为$ a_3 = 2 $ , $ a_8 = 4 $ 且 $ 2 < 3 < 4 < 8 $ .

题解

使\(f_i\)表示在\(j\in [i,n]\),有多少个满足\(a_j<j\)的,那么当找到一个\(a_i<i\)时,就可以让答案加上\(f_{i+1}\),这里需要注意,因为\(f_i\)可能会把\(a_i\)本身也算进去,所以需要加上\(f_{i+1}\)而不是\(f_i\)

参考代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int Maxn=2e5+10;
int n,ans;
int a[Maxn],f[Maxn];
void run()
{
	cin>>n;ans=0;memset(f,0,sizeof(f));
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++) if(a[i]<i) f[a[i]]++;
	for(int i=n;i>=1;i--) f[i]+=f[i+1];
	for(int i=1;i<=n;i++) if(a[i]<i) ans+=f[i+1];
	cout<<ans<<endl;
}
signed main()
{
	int t;
	cin>>t;
	while(t--) run();
}

标签:10,About,Satisfying,int,CF1703F,测试数据,leq,long,整数
From: https://www.cnblogs.com/lyk2010/p/17854773.html

相关文章

  • umount 报错umount: /new_room: target is busy. (In some cases useful info
    挂载逻辑卷后,尝试更新逻辑卷的文件系统[root@serverlost+found]#umount/new_room/umount:/new_room:targetisbusy.(Insomecasesusefulinfoaboutprocessesthatusethedeviceisfoundbylsof(8)orfuser(1))报错说繁忙上网查发现我进入......
  • About Me
    名字lht,英文名Creeper,十四岁。生日:2009.9.16坐标SC,成都外国语学校新初三OIer。CSP2023J&S都炸了。今年目标:Atcoder青名CodeforcesSpecialist2023.11.12注册博客园。Atcoder|Codeforces|洛谷|洛谷小号喜欢打和平精英,王者荣耀(ios微信&QQ:死宅混子......
  • 3 ways light pollution harms the planet - and what we can do about it
    Lightpollutionnotonlyimpactstheenvironment,butourhealthtoo. ·Globallightpollutionhasincreasedby49%over25yearsto2017,newresearchshows,andtherealfiguremaybeevenhigher.·Itsimpactsarewide-ranging-withhumanhealth,thee......
  • About Me
    Name:HexNy0aFormername:NightTac、LeiyNeKoAge:22SecurityAge:1.5Occupation: 武器开发GitHub: HexNy0a(HexNy0a)·GitHubOriginalCTFQuestions:GitHub-HexNy0a/DarkWeb_ChatRoom:CTFshow愚人杯,竞赛供题(CTF/WEB)GitHub-HexNy0a/EasyIMG:福建省第......
  • 题解 CF457F 【An easy problem about trees】
    尝试理解,感谢cz_xuyixuan的题解。算作是很多情况的补充说明。我们不妨先二分答案,将\(\gemid\)的设为\(1\),\(<mid\)的设为\(0\),于是问题转化为了权值均为\(0/1\)的版本。我们称一棵树的大小为其非叶节点数。我们称一棵大小为奇数的树为奇树,大小为偶数的树为偶树。对......
  • About Me
    本人cyf,坐标HN-CS,生日2011.12.08。喜欢打篮球、写题解,ydq是好基友。QQ:26424459由于基本上一天都不怎么碰家里的笔记本,所以可能一般不在线。洛谷号是cyfqwq,CF两个号,AT号是,有空就会打比赛。flagof2023:洛谷比赛等级分上\(1200\)(\(1167\)/\(1200\))CF小号......
  • About me
    AboutmeHA初三蒟蒻oier一枚qwqMyLuoguMycodeforcesMyAtcoderQQ:1741494316......
  • About how to use Char.GetNumericValue
    hemethodChar.GetNumericValueretrievesthenumericvalueofaspecificchar.However,it'simportanttonoteafewthingsaboutthismethod:It'sdesignedtoworkonindividualcharvalues,notonlistsorstrings.Themethodreturnsadouble......
  • about_auth
    title:浅谈一下前后端鉴权方式^.^tags:[前后端鉴权,Auth,JWT]categories:一般垃圾keywords:前后端鉴权,jwt,auth,token,php,javascriptdescription:浅谈一下前后端鉴权方式hot:truedate:2020-12-0517:05:50{%notesuccessno-icon%}虽然本人现在从事前端开......
  • 2023.9.24 ABout Math
    CF645F我们可以计算这样的函数\(F(x)\)表示\(\gcd\)是\(x\)的倍数有多少个\(k\)元组。设\(x\)的倍数有\(cnt_x\)个数,那么\(F(x)=C_{cnt_x}^k\)。根据莫反,\(f(x)=\sum_{x|d}F(d)\mu(d/x)\)\(Ans=\sumxf(x)=\sum_{x=1}^nx\sum_{x|d}\mu(d/x)\timesC_{cnt_d}......