ZOJ 3961 Let's Chat (双指针 + 区间交)
来源:互联网 发布:mac ftp 看不到文件夹 编辑:程序博客网 时间:2024/05/16 14:48
ACM (ACMers’ Chatting Messenger) is a famous instant messaging software developed by Marjar Technology Company. To attract more users, Edward, the boss of Marjar Company, has recently added a new feature to the software. The new feature can be described as follows:
If two users, A and B, have been sending messages to each other on the last m consecutive days, the “friendship point” between them will be increased by 1 point.
More formally, if user A sent messages to user B on each day between the (i - m + 1)-th day and the i-th day (both inclusive), and user B also sent messages to user A on each day between the (i - m + 1)-th day and the i-th day (also both inclusive), the “friendship point” between A and B will be increased by 1 at the end of the i-th day.
Given the chatting logs of two users A and B during n consecutive days, what’s the number of the friendship points between them at the end of the n-th day (given that the initial friendship point between them is 0)?
给定若干区间
解题思路
双指针求区间交问题。由于问题保证了 It is guaranteed that for all 1 ≤ i < x, ra, i + 1 < la, i + 1 and for all 1 ≤ i < y, rb, i + 1 < lb, i + 1. 故不需要进行区间合并。
对于 ia 表示的区间 a[ia] 和 ib 表示的区间 b[ib] ,其区间交为
之后判断 ia 指针或 ib 指针进行后移,继续判断新的两个区间的交。
代码
#include<bits/stdc++.h>using namespace std;int n, m, x, y, ans;pair<int, int> a[110], b[110], p;void solve(){ ans = 0; int ia = 1, ib = 1; p.first = max(a[ia].first, b[ib].first); while(true) { p.second = min(a[ia].second, b[ib].second); ans += max(0, p.second-p.first+1-m+1); if(ia > x || ib > y) return; if(p.second == a[ia].second && ia<=x) ia++; else if(p.second == b[ib].second && ib<=y) ib++; p.first = max(a[ia].first, b[ib].first); }}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d %d %d %d",&n,&m,&x,&y); for(int i=1;i<=x;i++) scanf("%d %d", &a[i].first, &a[i].second); for(int i=1;i<=y;i++) scanf("%d %d", &b[i].first, &b[i].second); solve(); printf("%d\n", ans); }}
- ZOJ 3961 Let's Chat (双指针 + 区间交)
- ZOJ 3961 Let's Chat
- 【ZOJ 3961 Let's Chat】
- zoj 3961 Let's Chat
- ZOJ 3961Let's Chat
- Let's Chat(区间)
- Let's Chat
- Let's Chat
- ZOJ3961 Let's Chat
- ZOJ3961-Let's Chat
- Let's Chat
- 省赛D Let's Chat
- ZOJ 3953 Intervals (贪心/区间交)
- ACM刷题之ZOJ————Let's Chat
- 浙江省赛 D Let's Chat
- 浙江省第十四次ACM程序竞赛 Let's Chat(D题)
- zoj 2507 Let's play a game
- 2017 浙江省赛 C What Kind of Friends Are You + D Let's Chat
- 【数据结构-队列】杂记
- CSS 计数器
- LeetCode题目392. Is Subsequence
- Swift字符串基本操作(二)
- volatile关键字
- ZOJ 3961 Let's Chat (双指针 + 区间交)
- 浅谈React实现评论框(二)
- Mac升级到macOS Sierra 10.12后SVN管理软件Conerstone破解版安装后提示“已损坏”问题
- 用curl命令上传文件和以post方式发请求
- jquery-countdown插件教程
- hadoop-2.6.5安装
- ReentrantReadWriteLock读写锁的使用
- SSM( Spring、SpringMVC、MyBatis )整合、SpringMVC参数绑定
- select_resultMap_关联查询_association定义关联对象封装规则,关联查询_分步查询&延迟加载