Codeforces - 1324D - Pair of Topics(尺取)

题目链接

题目大意:让你找出所有\(ai+aj > bi+bj\)(i > j)

??其实这题不用在意\(i > j\)只要把\(i\neq j\)并且符合条件的一对数记做一个答案就行了。。。(显然通过\(i和j\)交换必有\(i > j\))。然后我们把\(ai+aj > bi+bj\)变形可得\((ai-bi)+(aj-aj) > 0\)所以我们把所有的\(ai\)和\(bi\)做差

得到数组\(d\),那么只有\(d\)数组中存在一对数满足两数之和大于0就是答案的一个解,我们的任务就是计算\(d\)数组中有多少对这样的数。

??我们对数组\(d\)进行排序,如果一个数\(di\)与另一个比\(di\)小的数\(dj\)相加和大于\(0\),那么所有大于\(dj\)的数与\(di\)相加肯定都大于0,这些数的数量就是\(i-j\)。我们利用这个性质,尺取出所有的结果即可,如果想进一步降低时间复杂度。

还可以二分来找和\(di\)相加大于0的最小的\(dj\)。

//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl ‘\n‘
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("==================================================\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  998244353;
const int INF = 0x3f3f3f3f;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == ‘-‘)f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 2e5+10;
int a[maxn];
int main(void) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i<n; ++i)
        scanf("%d", &a[i]);
    for (int i = 0, b; i<n; ++i) {
        scanf("%d", &b);
         a[i] -= b;
    }
    sort(a, a+n);
    ll ans = 0; int l = 0, r = n-1;
    while(l<r) {
        while(l<r && a[l]+a[r]<=0) ++l;
        if (l>=r) break;
        ans += r-l;
        --r;
    }
    printf("%lld\n", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/shuitiangong/p/12567779.html

时间: 2024-10-05 22:37:14

Codeforces - 1324D - Pair of Topics(尺取)的相关文章

Codeforces 660 C. Hard Process (尺取)

题目链接:http://codeforces.com/problemset/problem/660/C 尺取法 1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 static int num[int(3e5 + 5)]; 6 int n, k; 7 scanf("%d %d", &n, &k); 8 for(int i = 1; i <= n; ++i) { 9 s

Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)

题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序列的长度是多少. 将相同颜色的下标存到对应颜色的容器中,比如ans[a[i]].push_back(i)就是将下标为i颜色为a[i]的数存到ans[a[i]]容器中. 对于每种颜色序列,尺取一下 在差距小于k的情况下取能取到的最大长度. 1 //#pragma comment(linker, "/S

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has got a robot which is situated on an infinite Cartesian plane, i

Codeforces 939E Maximize! (三分 || 尺取)

<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-mean(s)$表示这个集合的最大值与平均值的最大差值. 解题分析:首先,因为输入的$x$是非递减的,所以要使$max(s)-mean(s)$最大,肯定$max(s)$就是最后一个输入元素的大小.$x$已经确定了,现在就是尽可能的使$mean(s)$尽可能的小.如何使得平均值最小呢?肯定是从最前面的最小的元素开始

Codeforces Round #451 (Div. 2)【A,B,C,D,E】【C题:模拟 D题:尺取+贪心 E题:思维+优先队列维护最值】

特判最后一位即可 1 #include<bits/stdc++.h> 2 3 using namespace std; 4 #define int long long 5 6 signed main(){ 7 int n;cin>>n;int t=n%10; 8 if(t==0) cout<<n; 9 else if(t>5) { 10 cout<<(n+10-t); 11 } 12 else { 13 cout<<(n-t); 14 }

Codeforces Round #627 (Div. 3) D. Pair of Topics(二分/直接遍历)

The next lecture in a high school requires two topics to be discussed. The ii -th topic is interesting by aiai units for the teacher and by bibi units for the students. The pair of topics ii and jj (i<ji<j ) is called good if ai+aj>bi+bjai+aj>

NOJ 1072 The longest same color grid(尺取)

Problem 1072: The longest same color grid Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  %lld   Java class name:  Main Description There are n grid, m kind of color. Grid number 1 to N, color number 1 to M.  The color of

[HDOJ6119] 小小粉丝度度熊(尺取)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6119 合并有重叠的段后尺取. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <bi

POJ3061 Subsequence 尺取or二分

Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements o