CodeForces - 1324D Pair of Topics

题目大意:

这题大意ai+aj>bi+bj全在这个式子上,就问你满足的组合有几种,

题目分析:
整理一下,得到(ai-bi)+(aj-bj)>0 ,所以现在我们就只用做个差,然后找有几种组合就好了,
开始比较笨,tle了一发,后来才知道要分治一下,我们给做差后的数组排个序,每次找到比自己大的第一个数的下标,那么就可以很快的得到可以有几种组合

AC代码:

#include<iostream>
#include<cmath>
#include <cstring>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e6;
long long  s1[maxn],s=0,b=0,ans,a1[maxn],b1[maxn];
int t;
int main(){while(scanf("%d",&t)!=EOF){s=0;b=0;ans=0;for(long long i=1;i<=t;i++){scanf("%lld",&a1[i]);}for(long long i=1;i<=t;i++){scanf("%lld",&b1[i]);a1[i]-=b1[i];}sort(a1+1,a1+1+t);for(int i=1;i<=t;i++){long long cnt=upper_bound(a1+i+1,a1+t+1,-a1[i])-a1;//printf("%lld  ",cnt);ans+=t-cnt+1;//printf("%lld\n",ans);}printf("%lld\n",ans);}
}

我又有个问题:
现在问题解决了

当我数组下标从0开始的时候,upper_bound 返回的就不是第一个大于查询数的位置,而变成了大于等于,结果输出就不对了,这是为什么啊,
问题代码:

#include <cstring>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e6;
long long  s1[maxn],s=0,b=0,ans,a1[maxn],b1[maxn];
int t;
int main(){while(scanf("%d",&t)!=EOF){s=0;b=0;ans=0;for(long long i=0;i<t;i++){scanf("%lld",&a1[i]);}for(long long i=0;i<t;i++){scanf("%lld",&b1[i]);a1[i]-=b1[i];}sort(a1,a1+t);for(int i=0;i<t;i++){long long cnt=upper_bound(a1+i,a1+t,-a1[i])-a1;printf("%lld  ",cnt);ans+=t-cnt;printf("%lld\n",ans);//这里输出看看 }printf("%lld\n",ans),fflush(stdout);}
}

这个,其实是我没注意,代码写错了,我传进upper_bound的是-a[o]比它大的是他自己没错,,所以注意:upper_bound(a1+i+1,a1+t+1,-a1[i])-a1 这里查找的时候,其实是排除了它自己,如果下标从0开始,那么应该写成upper_bound(a1+i+1,a1+t,-a1[i])-a1,a1+1+i是从后一个开始查,但是a1+t就可以了,这是他的上界
所以修改后的从0开始的代码:

#include<iostream>
#include<cmath>
#include <cstring>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e6;
long long  s1[maxn],s=0,b=0,ans,a1[maxn],b1[maxn];
int t;
int main(){while(scanf("%d",&t)!=EOF){s=0;b=0;ans=0;for(long long i=0;i<t;i++){scanf("%lld",&a1[i]);}for(long long i=0;i<t;i++){scanf("%lld",&b1[i]);a1[i]-=b1[i];}sort(a1,a1+t);for(int i=0;i<t;i++){long long cnt=upper_bound(a1+i+1,a1+t,-a1[i])-a1;//printf("%lld  ",cnt);ans+=t-cnt;//printf("%lld\n",ans);//这里输出看看 }printf("%lld\n",ans),fflush(stdout);}
}

CodeForces - 1324D Pair of Topics (分治+排序)相关推荐

  1. [codeforces 1324D] Pair of Topics 分而治之+排列组合

    Codeforces Round #627 (Div. 3)   比赛人数6434 [codeforces 1324C]  Frog Jumps   一直向右+边界处理 总目录详见https://bl ...

  2. CodeForces - 1324D Pair of Topics(思维+二分)

    题目链接:https://vjudge.net/contest/362265#problem/D The next lecture in a high school requires two topi ...

  3. Codeforces 1324D Pair of Topics

    题目链接:https://codeforces.com/contest/1324/problem/D 题目描述 有两个长度为 n 的数组 A, B.问有多少对 (i,j) 满足 i < j 且 ...

  4. CodeForces - 1324D Pair of Topics(二分或双指针)

    题意:略 题记: 做法一:二分 #include<bits/stdc++.h>using namespace std; typedef long long ll; const int N= ...

  5. [Codeforces Round #627]1324D - Pair of Topics[二分]

    1324D - Pair of Topics[二分] time limit per test memory limit per test input output 2 seconds 256 mega ...

  6. cf 1324D. Pair of Topics

    D. Pair of Topics 题意:给定ab序列,问i<j且ai+aj>bi+bj的对数. 转化:ai-bi<-(aj-bj) 一开始拿到题目想着sort,但是发现i<j ...

  7. CodeForces - 1324 D. Pair of Topics 思维+多解法

    CodeForces - 1324 D. Pair of Topics 原题地址: http://codeforces.com/contest/1324/problem/D 基本题意: 给你一个数组, ...

  8. 优先队列如何按照pair 的第二关键字排序(对比vector按照pair第二关键字排序)

    按照pair的第二关键字排序需要自定义,需要注意的是优先队列priority_queue的重载和vector等是相反的,即 vector中是从小到大,到了优先队列是从大到小.当然,cmp的写法也有些不 ...

  9. Codeforces1324D Pair of Topics (思维 + 二分)

    题目链接: Pair of Topics 大致题意 给定两个数组a和b, 要求找出所有满足i < j 并且 ai + aj > bi + bj 的所有数对. 解题思路 我们不难想到先对等式 ...

最新文章

  1. 5G和AI机器人平台
  2. GridView中DropDownList联动
  3. 1420C1. Pokémon Army (easy version)
  4. AdPlayBanner:功能丰富、一键式使用的图片轮播插件
  5. python3.7 matplotlib增加坐标说明_python matplotlib:如何在极坐标图中的轴和刻度标签之间插入更多空格?...
  6. WPF in Finance
  7. Java学习之正则表达式
  8. 对天敏电视精灵I老版播放器的修改和分析
  9. 基于GNU Radio和HackRF/LimeSDR的802.11收发机
  10. 用JAVA时间类计算今天到指定日期的天数统计
  11. python爬虫12306查票
  12. 13万人12306信息泄露 小伙挨个发邮件通知
  13. C++STL容器的比较
  14. 【2022网易雷火】游戏研发笔试-AC代码及题目分享
  15. Promise 基本使用详解
  16. torch.repeat()
  17. kuka机器人三种运动编程
  18. 程序员高效率工作工具归纳(上)
  19. 手机USB共享电脑宽带的尝试过程,从失败到成功
  20. 1024 发福利,送你一份珍藏依旧的 Java,大数据礼包,确定不收藏 ?拒绝白嫖 !

热门文章

  1. 用Scrapy框架爬取豆瓣电影,构建豆瓣电影预测评分模型
  2. OSChina 周日乱弹 ——我是胖娜丽莎
  3. 5G关键技术,D2D通信-ielab
  4. 微信小程序开发学习—Day1
  5. Uni-App - 使用 iconfont 图标或者自定义图标
  6. Android开发之使用createFromStream加载图片发现图片变小
  7. 九度oj-1158-买房子
  8. scp_linux之间互传文件
  9. android 微信文件位置改变大小,安卓微信8.0.6内测更新:发文件大小上调1G等6个变化!...
  10. 谁又在乎过你呢“main()之泪伤”