P3406 Submarine high-speed rail (differential)

Title description

Insert picture description here
Insert picture description here

analysis

Find out the number of passes of each section of the distance by difference, and find out which scheme is cheaper according to a, b, c

for(int i = 1;i<m;i++){
    
    
		int x= min(a[i],a[i+1]),y = max(a[i],a[i+1]);
		b[  x  ] += 1;
		b[  y  ] -= 1;	
	} 

ps. Because it is the pair distance difference, it is b[y]--, not b[y+1]--.

Code

#include<iostream>
#include<stdlib.h>
#include<string>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;
const int maxn = 1e5+50;
long long n,m,a[maxn],b[maxn],t1,t2,t3;
long long ans;
int main(){
    
    
	//freopen("a.txt","r",stdin);
	cin>>n>>m;
	for(int i =1;i<=m;i++){
    
    
		cin>>a[i];
	}
	
	//差分标记 
	for(int i = 1;i<m;i++){
    
    
		int x= min(a[i],a[i+1]),y = max(a[i],a[i+1]);
		b[  x  ] += 1;
		b[  y  ] -= 1;	
	} 
	
	//前缀和,得出每段路经过的次数 
	for(int i = 1;i<=n-1;i++){
    
    
		b[i] += b[i-1];
	} 
	
	for(int i = 1;i<=n-1;i++){
    
    
		cin>>t1>>t2>>t3;
		long long pa = t1*b[i],pb = t3 + t2*b[i];
		ans += min(pa,pb);
		//cout<<ans<<endl;
	} 
	cout<<ans<<endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_45210226/article/details/108410162