列表

详情


NC221647. What'sMineisMine

描述

The hot new video game “Mining Simulator” has just been released. In the game, rare earth mineral ores appear at certain times and you can mine them when they appear. After mining, you can later turn in the minerals for money. The quantity of mineral available during an appearance is proportional to the duration of the appearance and the price per unit of each mineral is decided beforehand.

The game contains a geological sensor that determines times when mineral ores will appear during a given day and at the beginning of each day you have a price list for each mineral. Assuming you mine out the mineral in exactly the amount of time that it is available, you cannot partially mine minerals (you must either not mine any of a given occurrence or mine all of it) and you can only mine one ore occurrence at a time.

Given a list of the prices of ݉m minerals and n ore occurrences during a day, write a program to output the maximum amount of money you can earn from mining on that day. The mineral amount is the appearance time (end time – start time). You can mine an ore right after finishing the previous mining. In other words, one mined-ore’s end time can be same as another mined-ore’s start time. In the case depicted in Figure L.1, if you choose the mineral of type 1 that appears at time 2 and disappears at time 5, then the mineral amount is 5 − 2 = 3 and you earn 3 × 2 = 6. Next, if you choose the mineral of type 2 that appears at time 7 and disappears at time 11, then the mineral amount is 11 − 7 = 4 and you earn 4 × 3 = 12. Therefore, you earn 18 in total.

输入描述

Your program is to read from standard input. The input starts with a line containing two integers, ݉m and n (1 ≤ m ≤100, 1 ≤n ≤ 10,000), where ݉m is the number of types of minerals and n is the number of ore occurrences during the day. The mineral types are labeled from 1 to ݉m. The following ݉m lines contain a single integer corresponding to the price of one unit of the i-th mineral type on that day (the price is between 1 and 10,000). The following n lines represent ore occurrences. Each line contains three integers, s, e݁, t where s is the start time, e is the end time and t is the mineral type in each ore occurrence with 0 < s < e <  15,000  and  1 ≤ t ≤ m. The amount of mineral at each occurrence is ݁e - s.

输出描述

Your program is to write to standard output. Print exactly one line. The line should contain the maximum amount of money that can be earned on that day.

The following shows sample input and output for three test cases.

示例1

输入:

2 5
2
3
2 5 1
4 5 2
4 6 1
7 11 2
6 10 1

输出:

18

示例2

输入:

3 5
2
3
1
1 4 1
3 6 3
5 8 2
7 10 1
9 12 2

输出:

24

示例3

输入:

5 7
1
2
3
4
5
1 5 2
3 8 1
2 4 3
3 9 2
4 10 5
7 11 4
5 7 3

输出:

36

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3(3.9) 解法, 执行用时: 55ms, 内存消耗: 4772K, 提交时间: 2021-05-05 14:42:12

m,n=map(int,input().split())
p=[0]
for i in range(m):
    p.append(int(input()))
mi=[]
for i in range(n):
    mi.append([int(x) for x in input().split()])
mine=sorted(mi,key=lambda x:x[1])
l=len(mine)
dp=[0]*(mine[l-1][1]+1)
c=0
for i in range(1,mine[l-1][1]+1):
    for j in range(c,l):
        if mine[j][1]<=i:
            dp[i]=max(dp[mine[j][0]]+(mine[j][1]-mine[j][0])*p[mine[j][2]],dp[i])
        else:
            c=j
            break
    dp[i]=max(dp[i-1],dp[i])
print(dp[mine[l-1][1]])

C++(clang++11) 解法, 执行用时: 227ms, 内存消耗: 32504K, 提交时间: 2021-05-05 14:25:49

#include<bits/stdc++.h>
using namespace std;
int brr[10005];


int arr[10005][10005];
int maxx=-10;
int main() 
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	cin>>brr[i];
	
	for(int i=1;i<=m;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		arr[a][b]=abs(a-b)*brr[c];
		maxx=max(maxx,b);
	}
	for(int i=1;i<=maxx;i++)
	{
		for(int j=1;j<=i;j++)
		{
			arr[1][i]=max(arr[1][i],arr[1][j]+arr[j][i]);
		}
	}
	printf("%d\n",arr[1][maxx]);
}

上一题