Japan
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19724 | Accepted: 5340 |
Description
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)
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
题意:
给出 T, 代表有 T 个 case。后每个 case 给出 N,M,K 三个数,代表东边有 N 个城市,代表西边有 M 个城市,后给出 K 条路,代表东边的城市 a 连 西边的城市 b。求出一共有多少个相交点。
思路:
树状数组。求逆序数,对 x 由小到大排序,若 x 相同,则对 y 由小到大排序。对于每个 j ,求出 sum(no [ i ].y )的个数,就是 i < j ,a[ i ] <= a[ j ] 的个数。所以 j - sum ( no [ i ]. y ) 代表逆序数的个数。要用long long,不然会WA。
AC:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; typedef struct { ll x, y; } node; node no[1005 * 1005]; int n, m, k; ll bit[1005]; bool cmp(node a, node b) { if (a.x != b.x) return a.x < b.x; return a.y < b.y; } void add(ll i, ll x) { while (i <= m) { bit[i] += x; i += i & -i; } } ll sum(ll i) { ll s = 0; while (i > 0) { s += bit[i]; i -= i & -i; } return s; } int main() { int t; scanf("%d", &t); for (int tt = 1; tt <= t; ++tt) { memset(bit, 0, sizeof(bit)); scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < k; ++i) { scanf("%d%d", &no[i].x, &no[i].y); } sort(no, no + k, cmp); ll ans = 0; for (ll i = 0; i < k; ++i) { ans += (i - sum(no[i].y)); add(no[i].y, 1); } printf("Test case %d: %lld\n", tt, ans); } return 0; }
相关推荐
在Java编程中,数组是一种基本的数据结构,用于存储固定数量的同类型元素。然而,在某些场景下,我们可能需要动态地改变数组的长度,以适应不断变化的数据需求。本文将介绍如何在Java中实现动态改变数组长度以及如何...
颜色配置文件,用于进行色彩管理,多运用于印刷领域,是重要的颜色配置文件。
日本区域geojson数据 {"type":"Feature","properties":{"NAME":"沖縄県"},"geometry":{"type":"MultiPolygon","coordinates":[[[[122.7141754,24.4498321],[122.7145737,24.4596961],[122.7154535,24.4696961],……...
标题“japan Words1-12”和描述中提到的内容似乎与日语词汇学习资源相关,可能是一个包含12个部分或单元的日语词汇教学材料。标签同样为“japan Words1-12”,进一步证实了这个主题。压缩包子文件的文件名称列表显示...
Harris 角点检测 FPGA实现 Tak Lon Chao, Kin Hong Wong, "An efficient FPGA implementation of the Harris Corner feature detector" Code:in VHDL and Verliog running on Zedboard
很抱歉,但根据您给出的信息,"Japan_word 13-24"似乎是某种音乐专辑或者音频集合的标题,描述中重复的短语并没有提供额外的上下文信息。标签也与标题相同,而压缩包子文件的文件名称列表看起来是一系列音频文件...
Japan TV Live_1.1.0_apkcombo.com.apk.1.1.1
《A Song For Japan》是一首特别的音乐作品,创作背景是为了向2011年3月11日发生的日本东北地方太平洋近海地震及其引发的巨大海啸中的受灾民众表达慰问和支持。这首乐曲由作曲家精心打造,旨在通过音乐传递温暖与...
在“japan_stream.zip”这个压缩包中,我们得以深入探索日本的河流网络,包括一级、二级和三级河流的信息,以及河流的名称等关键数据。这些数据对于地理研究、水资源管理、环境保护以及城市规划等领域都具有重要意义...
这种能力使得JavaScript的对象可以成为复杂数据结构的基础,如树形结构、图形结构等。 总之,JavaScript中的关联数组是一种强大的数据结构,它以键值对的形式存储数据,提供了高度的灵活性和可读性。通过理解如何...
Age of Japan 2 continues oriental theme, that you like so much. Classic match three blends together relaxing music, nice and harmonious gameplay. You’ll pass a lot of time enjoing this game. The game...
Hive嵌套JSON Arrray UDF 此UDF接收“ JSON字符串”和JSON数组的路径,并收集此路径指定的所有元素(也处理嵌套的JSON数组)。 例子: 假设此JSON在某些表的行中: {" request " : {" user " : " Mario " ," ...
Japan
japan.3b071018.exe,japan.3b071018.exe
X Japan - Tears的吉他谱,网上貌似不好找,喜欢的朋友下吧 ENJOY`````
"日本" => "Japan", // ... ); ``` 这样的数组可以在项目中轻松导入和使用,只需要在需要的地方通过`require`或`include`语句引入这个文件。然后,你可以遍历数组,根据需求获取或显示国家的中英文名称。例如,...
本压缩包“acm比赛题目(2006japan.f)”显然是2006年日本ACM区域赛的题目集合,其中的文件名为"problems",很可能包含了一系列编程题目和相关说明。 在ACM比赛中,参赛队伍需要在5个小时内解决一系列复杂的算法问题...
【文章标题】:挑战通路巨人--am/pm Japan差异化的行销战略 【文章描述】:本篇文章探讨了am/pm Japan如何在日本便利商店市场中挑战行业领导者seven-eleven Japan,通过实施独特的营销策略来吸引不同的顾客群体,...
很抱歉,但根据您给出的信息,标题 "For Japan Farmers, Radiation Fears Mean Economic Pain" 显然是关于日本农民在核辐射担忧下所遭受的经济困难,这与IT行业的"源码"和"工具"标签并不直接相关。同时,提供的...