1952. Blonde and N cows

Posted by lemmin on Sat, 29 Jan 2022 14:34:47 +0100

Title Link

1952. Blonde and N cows

You may have heard the classic story about Goldilocks and three bears.

However, little is known that Goldilocks eventually became a farmer.

On her farm, there are \ (N \) cows in her cowshed.

Unfortunately, her cows are quite sensitive to temperature.

For cows \ (I \), the temperature that makes them feel comfortable is \ (A_i... B_i \).

If Goldilocks sets the temperature \ (t \) of the barn thermostat to \ (T < a_i \), the cow will feel cold and produce \ (X \) units of milk.

If she sets the thermostat temperature \ (T \) to \ (A_i ≤ T ≤ B_i \), the cow will feel comfortable and produce \ (Y \) units of milk.

If she sets the thermostat temperature \ (t \) to \ (T > b_i \), the cow will feel hot and produce \ (Z \) units of milk.

As expected, the value of \ (Y \) is always greater than \ (X \) and \ (Z \).

Given \ (X,Y,Z \) and the comfortable temperature range for each cow, please calculate the maximum milk yield that blondie can obtain by reasonably setting the thermostat temperature.

The thermostat temperature can be set to any integer.

Input format

The first line contains four integers \ (N,X,Y,Z \).

The next \ (N \) line contains two integers \ (A_i \) and \ (B_i \).

Output format

Output the maximum milk yield available.

Data range

\(1≤N≤20000\),
\(0≤X,Y,Z≤1000\),
\(0≤A_i≤B_i≤10^9\)

Input sample:

4 7 9 6
5 8
3 4
13 20
7 10

Output example:

31

Example explanation

Goldilocks can set the thermostat temperature to \ (7 \) or \ (8 \), which will make the cows \ (1 \) and \ (4 \) feel comfortable, the cows \ (2 \) feel hot and the cows \ (3 \) feel cold.

A total of \ (31 \) units of milk can be obtained.

Problem solving ideas

Difference, discretization

Consider each temperature interval as three intervals. Add \ (x \) to each number in the left interval, add \ (y \) to the middle interval, and add \ (z \) to the right interval. Then the problem is to select the number with the largest weight among all. Obviously, the difference can be used. Note that the left and right boundaries need to be added, which is easier to deal with. In addition, due to the large data range, it should also be discretized

  • Time complexity: \ (O(nlogn) \)

code

// Problem: blonde and N cattle
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/1954/
// Memory Limit: 64 MB
// Time Limit: 1000 ms

// %%%Skyqwq
#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }

template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=2e4+5,inf=0x3f3f3f3f;
int n,x,y,z;
pair<int,int> a[N];
int b[2*N];
vector<int> xs;
int main()
{
	scanf("%d%d%d%d",&n,&x,&y,&z);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].fi,&a[i].se);
		xs.push_back(a[i].fi);
		xs.push_back(a[i].se);
	}
	xs.push_back(inf),xs.push_back(-inf);
	sort(xs.begin(),xs.end());
	xs.erase(unique(xs.begin(),xs.end()),xs.end());
	int m=xs.size();
	int res=0,sum=0;
	for(int i=1;i<=n;i++)
	{
		int pos1=lower_bound(xs.begin(),xs.end(),a[i].fi)-xs.begin();
		int pos2=lower_bound(xs.begin(),xs.end(),a[i].se)-xs.begin();
		b[0]+=x,b[pos1]-=x;
		b[pos1]+=y,b[pos2+1]-=y;
		b[pos2+1]+=z,b[m-1]-=z;
	}
	for(int i=0;i<m;i++)
		sum+=b[i],res=max(res,sum);
	printf("%d",res);
	
	return 0;
}