Sicily 8843. Ranking and Friendship
来源:互联网 发布:中文翻译缅甸语软件 编辑:程序博客网 时间:2024/06/05 08:35
8843. Ranking and Friendship
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
Since teacher Herkabe has started ranking his N students, the number of friendships in his class has sharply fallen. The students near the bottom of the rankings list have become jealous of the top students, while the top students started looking down on their less successful colleagues.
According to Malcolm's observations, the following rule holds: two students are friends if their ranks are close enough, more precisely, if they differ by at most K. For example, if K = 1, then only neighbouring students on the rankings list are friends. Furthermore, two students are good friends if they are friends and their names have the same length.
Write a program to calculate the number of pairs of good friends in this gifted class.
Input
The first line of input contains two positive integers, N (3 ≤ N ≤ 300 000) and K (1 ≤ K ≤ N), from the problem statement.
Each of the following N lines contains a single student's name. The names are given in the order they appear on the rankings list. They consist of between 2 and 20 (inclusive) uppercase English letters.
Output
The first and only line of output must contain the required number of pairs.
Sample Input
样例1:4 2IVAIVOANATOM样例2:6 3CYNTHIALLOYDSTEVIEKEVINMALCOLMDABNEY
Sample Output
样例1:5样例2:2
Problem Source
2013年每周一赛第七场/COCI 2012.12
// Problem#: 8843// Submission#: 3557694// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <algorithm>using namespace std;struct man { int rank, length;};man M[300005];bool cmp(const man & m1, const man & m2) { if (m1.length == m2.length) return m1.rank < m2.rank; return m1.length < m2.length;}inline int ABS(int a) {if (a < 0) return -a; return a;}int main() { int N, K; scanf("%d%d\n", &N, &K); char name[25]; int l = 0; for (int i = 0; i < N; i++) { M[i].rank = i; gets(name); l = 0; while (name[l]) l++; M[i].length = l; } sort(M, M + N, cmp); int startPos = 0, endPos = 0; long long ans = 0; for (int i = 1; i < N; i++) { if (M[startPos].length == M[i].length) if (ABS(M[startPos].rank - M[i].rank) <= K) { ans += endPos - startPos + 1; endPos++; } else { while (startPos <= endPos && ABS(M[startPos].rank - M[i].rank) > K) startPos++; ans += endPos - startPos + 1; endPos++; } else startPos = endPos = i; } printf("%lld\n", ans); return 0;}
- Sicily 8843. Ranking and Friendship
- sicily 8843 Ranking and Friendship
- Bear and Friendship Condition
- Searching and Ranking
- codeforces Bear and Friendship Condition
- Bear and Friendship Condition CodeForces
- Friendship
- Friendship
- Friendship
- Friendship
- Friendship
- codeforces 791B Bear and Friendship Condition
- (codeforces)A. Bear and Friendship Condition
- Codeforces791 B. Bear and Friendship Condition
- Ranking
- Computer Security Conference Ranking and Statistic
- Computer Security Conference Ranking and Statistic
- Friendship and mobility: user movement in location-based social networks
- 将一个数字字符串转换成该字符串对应的数字(包括正整数、负整数)
- 文件上传和表单在同一个页面,先上传后提交表单
- jvm_参数含义
- 哈理工校园编程练习赛杭电 acm F. 4452 Running Rabbits
- hdu4990---Reading comprehension(矩阵快速幂)
- Sicily 8843. Ranking and Friendship
- vi/vim常用命令
- IE11中OCX控件的事件不能够被IE捕获的解决办法
- Hibernate之一 HelloWorld
- Sicily 2682. Die Hard 4
- mysql事务隔离级别
- 关于大型网站技术演进的思考(七)--存储的瓶颈(7)
- ViewPager之使用PagerTabStrip添加标题栏,实现滑动效果
- Stack (Linked List)