A sports company is planning to advertise in a tournament. It is a single round-robin tournament, that's to say competitors play with all the others once. The company thinks that the advertising impact is proportional to the so called competitiveness degree(CD) of the tournament.

CD is calculated in the following way: We assume there're N competitors in the tournament and of course N×(N−1)2 matches in total. For each competitor we define two values S and E which stand for skill and experience. We say a match between competitor i and competitor j is competitive if and only if Si+Ei≥Sj and Sj+Ej≥Si. CD equals to the number of competitive matches among all N×(N−1)2 matches in the tournament.

Input

One integer T (T≤20) on the first line indicates the number of cases. The first line of each case contains one integer N (1≤N≤10000) -- the number of competitors. Then N lines follows. The ithline contains two integer Si and Ei (1≤Si,Ei≤100000000) which are defined in the description.

Output

For each case, print the value of CD on a line.

Sample input and output

Sample Input Sample Output
3
2
1 2
4 1
2
1 2
2 2
5
1 9
5 4
3 4
2 2
6 2
0
1
8

Source

The 8th UESTC Programming Contest Final
 
解题报告
题目意思很简单,找出给出这些整数中有多少队满足
Si+Ei≥Sj and Sj+Ej≥Si
第一想法是暴力,too simple...结果肯定是TLE
How to slove?
我们先对Si + Ei 按照从大到小排序,
对于排序后的序列来讲,若有i < j,若i能到pos位,则j能到达的位置肯定<=pos
因此维护一个S单调不减,每次更新答案即可.
注意到我们不是一次把某队数满足的所有其他队数加上去,而是不断累加(类似于将来的值)
 
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set> using namespace std;
const int maxn = 1e4 + ;
int n; multiset<int>s; typedef struct data
{
int s,e;
friend bool operator < (const data & x,const data & y)
{
return x.s + x.e > y.s + y.e;
}
}; data A[maxn]; int main(int argc ,char * argv[])
{
int Case;
scanf("%d",&Case);
while(Case--)
{
int ans = ;
scanf("%d",&n);
for(int i = ; i < n ; ++ i)
scanf("%d%d",&A[i].s,&A[i].e);
sort(A,A+n);
s.clear();
s.insert(A[].s);
set<int>::iterator it = s.begin();
int cot = ;
for(int i = ; i < n ; ++ i)
{
int temp = A[i].s + A[i].e;
while( s.size() > && *it > temp)
{
s.erase(it--);
cot--;
}
ans += cot++;
s.insert(A[i].s);
if (A[i].s >= *it)
it++;
}
cout << ans << endl;
}
return ;
}

UESTC_Tournament CDOJ 124的更多相关文章

  1. cdoj 1489 老司机采花

    地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others)     M ...

  2. NYOJ题目124中位数

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAssAAAJUCAIAAABsWvwaAAAgAElEQVR4nO3dPXLjuraG4TsJ5xqIYw

  3. Error -27780: [GENERAL_MSG_CAT_SSL_ERROR]connect to host "124.202.213.70" failed: [10054] Connection reset by peer [MsgId: MERR-27780]

    解决方案一: 备注: 此方案如果请求响应时间太长,勾选"WinInet replay instead of Sockets(Windows only)"将会导致如下错误:

  4. TypeError: argument to reversed() must be a sequence ERROR basehttp 124 "GET /admin/ HTTP/1.1" 500 114103 Performing system checks...

    Error Msg TypeError: argument to reversed() must be a sequence ERROR basehttp 124 "GET /admin/ ...

  5. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

  6. SGU 124. Broken line 射线法 eps的精准运用,计算几何 难度:3

    124. Broken line time limit per test: 0.25 sec. memory limit per test: 4096 KB There is a closed bro ...

  7. 编写高质量代码改善C#程序的157个建议——建议124:考虑在命名空间中使用复数

    建议124:考虑在命名空间中使用复数 如果有一组功能相近的类型被分到了同一个命名空间想,可以考虑为命名空间使用复数. 最典型的例子有,在FCL中,我们需要把所有的非泛型集合类集中在一起存放,所以就有了 ...

  8. Loj #124. 除数函数求和

    链接:https://loj.ac/problem/124 就是筛一下积性函数. #include<bits/stdc++.h> #define ll long long #define ...

  9. CDOJ 1324 卿学姐与公主(分块)

    CDOJ 1324 卿学姐与公主(分块) 传送门: UESTC Online Judgehttp://acm.uestc.edu.cn/#/problem/show/1324 某日,百无聊赖的卿学姐打 ...

随机推荐

  1. 通用Makefile

    本文推荐了一个用于对 C/C++ 程序进行编译和连接以产生可执行程序的通用 Makefile. 在使用 Makefile 之前,只需对它进行一些简单的设置即可:而且一经设置,即使以后对源程序文件有所增 ...

  2. Spring MVC Controller 单元测试

    简介 Controller层的单元测试可以使得应用的可靠性得到提升,虽然这使得开发的时间有所增加,有得必失,这里我认为得到的比失去的多很多. Sping MVC3.2版本之后的单元测试方法有所变化,随 ...

  3. 自定义的string类

    头文件Hi_String.h #include<iostream> #include<string.h> using namespace std; class Hi_Strin ...

  4. Linux常用命令及使用技巧

    本文重点讲述Linux命令的使用,命令是学习Linux必须熟练掌握的一个部分.Linux下的命令大概有600个,而常用的命令其实只有80个左右,这些常用的命令是需要灵活掌握的.虽然Linux的各个发行 ...

  5. Windows无需CygWin 使用NDK开发

    无需cygwin,使用NDK进行开发   NDK从7开始自带编译器,在windows上无需配置cygwin的环境. 在eclips中配置NDK路径 在eclipse中点击菜单栏window-Perfe ...

  6. 【转】ffserver用法小结

    我们可以通过ffserver以及ffmpeg做一个简单的视频监控系统,ffserver用于视频的转发调度,ffmpeg用于转码 而对于ffserver最基本也是最重要的就是对它的ffserver.co ...

  7. input里面check 状态检测

    if($("#check_status").attr('checked')) //检测checkbox状态 { //checked状态 }else { //不是checked状态 ...

  8. Unity 命令行参数

    通常情况下,Unity可以通过双击桌面上的图标启动,也可以通过输入命令行启动(例如,MacOS终端或者Windows的CMD窗口),通过这种方式在启动时会接受命令和信息.我们可以制作一些小工具跟Uni ...

  9. Linux查看系统信息

    系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...

  10. 批量设置AssetBundleName

    using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class Change ...