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. 12 个 Linux 进程管理命令介绍

    执行中的程序在称作进程.当程序以可执行文件存放在存储中,并且运行的时候,每个进程会被动态得分配系统资源.内存.安全属性和与之相关的状态.可以有多个进程关联到同一个程序,并同时执行不会互相干扰.操作系统 ...

  2. python高级编程(第12章:优化学习)2

    #优化策略 #3个原则 """ 1a:寻找其他原因:确定第三方服务器或资源不是问题所在 2a:度量硬件:确定资源足够用 3a:编写速度测试:创建带有速度要求的场景 &qu ...

  3. NYOJ130 同样的雪花 【Hash】

    同样的雪花 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 You may have heard that no two snowflakes are alike. ...

  4. [Hapi.js] POST and PUT request payloads

    hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...

  5. jquery的验证实例方法

    上一篇介绍了js的正则验证法,这一片就用jquery来实例运行一下其中的几个方法 Js部分 <script> $(function(){ var ok1=false; var ok2=fa ...

  6. Javascript中使用replace()方法+正则表达式替换掉所有字符

    Js中的replace方法,只能替换掉第一次匹配到的字符,   而我们经常需要替换一个字符串中所有的匹配字符,这时候可以用正则表达式: str.replace(/a/g,"b"); ...

  7. 不同浏览器对URL最大长度的限制(转)

    1.今天碰到一个bug,window.open后面的页面,接收参数不全,导致后台报错.实验了一下.发现是使用get方法请求服务器时,URL过长所致 微软官方的说明: http://support.mi ...

  8. JSP脚本元素上机手册

    L3 <JSP基础>上机手册 内容回顾 脚本元素<%! %> <%= %> <% %> 注释元素 JSP指令元素 JSP动作元素 上机目标 掌握脚本元素 ...

  9. C#控制条码打印机 纸张大小,间距,绘制内容(所有条码打印机通用)

    其他条码知识 请访问:http://www.ybtiaoma.com ,本文仅供参考,请勿转载,谢谢 using System; using System.Drawing; using System. ...

  10. JDBC学习入门

    一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...