473人阅读 评论(0)

# 问题

1）去掉[2, 5]的话，剩下的区间覆盖的整数点为3,4,5,6,7，有5个；
2）去掉[3, 5]的话，剩下的区间覆盖的整数点为2,3,4,5,6,7，有7个；
3）去掉[4, 7]的话，剩下的区间覆盖的整数点为2,3,4,5，有4个。

# 思路

(secondTail, Tail]这段区间表示还没被其它区间覆盖到的！所以想要计算每个区间对于整体的独立贡献的话，用这个区间来计算就好了。

## 更新独立贡献值

secondTail < F <= S <= Tail (secondTail, F) S Tail
secondTail < F <= Tail < S (secondTail, F) Tail S
secondTail <= Tail < F <= S (secondTail, Tail] F S

## 更新secondTail和Tail的值

F <= S <= secondTail <= Tail secondTail Tail
F <= secondTail < S <= Tail S Tail
F <= secondTail <= Tail < S Tail S

## 细节

if (S >= Tail)
secondTail = max(Tail, F);
else
secondTail = max(secondTail, S);
Tail = max(Tail, S);

# 代码

LL cover(RangeList& intervals) {
// 注意初始值
LL Tail = intervals[0].first - 1, secondTail = Tail, TailIndex = -1;
vector<LL> maxPoints(intervals.size());

for (int i = 0; i < intervals.size(); ++i) {
// 更新独立贡献值
if (i > 0 && intervals[i].first > secondTail) {
maxPoints[TailIndex] += min(Tail, intervals[i].first - 1) - (secondTail + 1) + 1;
}

// 更新secondTail值
if (Tail > intervals[i].second)
secondTail = max(secondTail, intervals[i].second);
else
secondTail = max(Tail, intervals[i].first-1);

// 更新Tail值
if (intervals[i].second > Tail) {
Tail = intervals[i].second;
TailIndex = i;
}
}

// 别忘了最后还得再算一次！
maxPoints[TailIndex] += Tail - (secondTail+1) + 1;

// 找出最大的独立贡献值
LL maxPoint = 0;
for (int i = 0; i < maxPoints.size(); ++i) {
maxPoint = max(maxPoint, maxPoints[i]);
}
return maxPoint;
}

#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
using namespace std;

typedef long long LL;
typedef vector<pair<LL, LL> > RangeList;

LL coverAll(RangeList& intervals) {
LL left = intervals[0].first, right = intervals[0].second;
LL area = 0;
for (int i = 1; i < intervals.size(); ++i) {
// 前面自成一个区间，那么就此分开
if (intervals[i].first > right) {
area += right - left + 1;
left = intervals[i].first;
right = intervals[i].second;
} else if (intervals[i].second > right) {
right = intervals[i].second;
}
}
area += right - left + 1;

return area;
}

LL cover(RangeList& intervals) {
// 注意初始值
LL Tail = intervals[0].first - 1, secondTail = Tail, TailIndex = -1;
vector<LL> maxPoints(intervals.size());

for (int i = 0; i < intervals.size(); ++i) {
// 更新独立贡献值
if (i > 0 && intervals[i].first > secondTail) {
maxPoints[TailIndex] += min(Tail, intervals[i].first - 1) - (secondTail + 1) + 1;
}

// 更新secondTail值
if (Tail > intervals[i].second)
secondTail = max(secondTail, intervals[i].second);
else
secondTail = max(Tail, intervals[i].first-1);

// 更新Tail值
if (intervals[i].second > Tail) {
Tail = intervals[i].second;
TailIndex = i;
}
}

// 别忘了最后还得再算一次！
maxPoints[TailIndex] += Tail - (secondTail+1) + 1;

// 找出最大的独立贡献值
LL maxPoint = 0;
for (int i = 0; i < maxPoints.size(); ++i) {
maxPoint = max(maxPoint, maxPoints[i]);
}
return maxPoint;
}

int main() {
int t;
cin >> t;
for (int time = 1; time <= t; ++time) {
LL N, L1, R1, A, B, C1, C2, M;
cin >> N >> L1 >> R1 >> A >> B >> C1 >> C2 >> M;
RangeList intervals;
for (int i = 0, x = L1, y = R1; i < N; ++i) {
intervals.push_back(make_pair(min(x, y), max(x, y)));
LL x_last = x, y_last = y;
x = (A * x_last + B * y_last + C1) % M;
y = (A * y_last + B * x_last + C2) % M;
}
sort(intervals.begin(), intervals.end());
cout << "Case #" << time << ": " << coverAll(intervals) - cover(intervals) << endl;
}

return 0;
}
2
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：117709次
• 积分：2589
• 等级：
• 排名：第14148名
• 原创：142篇
• 转载：21篇
• 译文：1篇
• 评论：24条
最新评论