首页 技术 正文
技术 2022年11月14日
0 收藏 670 点赞 3,940 浏览 2062 个字

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, … from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.

Input

The input file starts with T – the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.

Output

For each test case write one line on the standard output: 
Test case (case number): (number of crossings)

Sample Input

1
3 4 4
1 4
2 3
3 2
3 1

Sample Output

Test case 1: 5
题意:给一组成对的点求相交的线段
题解:逆序数(虽然我也不知道这是个啥),先排序按右边小的排,右边相同按左边小的排,保证不会因为右边点相同而计算重复,然后求sum(N)-sum(s[i].b)(好像是逆序数);
一开始思路都是对的,结果写太搓了,TLE了,无奈之下看题解,居然和我写的一模一样。。。。(代码能力太差》。《)
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1using namespace std;const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f;struct edge{
int a,b;
}s[N*N];
int c[N];bool comp(const edge &x,const edge &y)
{
if(x.a!=y.a)return x.a<y.a;
return x.b<y.b;
}
void add(int i)
{
while(i<N){
c[i]++;
i+=i&(-i);
}
}
ll sum(int i)
{
ll ans=;
while(i>){
ans+=c[i];
i-=i&(-i);
}
return ans;
}
int main()
{
/* ios::sync_with_stdio(false);
cin.tie(0);*/
// cout<<setiosflags(ios::fixed)<<setprecision(2);
int t,cnt=,n,m,k;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
memset(c,,sizeof c);
for(int i=;i<k;i++)scanf("%d%d",&s[i].a,&s[i].b);
sort(s,s+k,comp);
ll ans=;
for(int i=;i<k;i++)
{
add(s[i].b);
ans+=(sum(N)-sum(s[i].b));
}
printf("Test case %d: %lld\n",++cnt,ans);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295