欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Educational Codeforces Round 106 (Div. 2) C. Minimum Grid Path(思维 枚举 贪心)

程序员文章站 2022-05-09 21:13:56
...

题目链接:https://codeforces.com/contest/1499/problem/C

Let’s say you are standing on the XY-plane at point (0,0) and you want to reach point (n,n).

You can move only in two directions:

to the right, i. e. horizontally and in the direction that increase your x coordinate,
or up, i. e. vertically and in the direction that increase your y coordinate.
In other words, your path will have the following structure:

initially, you choose to go to the right or up;
then you go some positive integer distance in the chosen direction (distances can be chosen independently);
after that you change your direction (from right to up, or from up to right) and repeat the process.
You don’t like to change your direction too much, so you will make no more than n−1 direction changes.

As a result, your path will be a polygonal chain from (0,0) to (n,n), consisting of at most n line segments where each segment has positive integer length and vertical and horizontal segments alternate.

Not all paths are equal. You have n integers c1,c2,…,cn where ci is the cost of the i-th segment.

Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of k segments (k≤n), then the cost of the path is equal to ∑i=1kci⋅lengthi (segments are numbered from 1 to k in the order they are in the path).

Find the path of the minimum cost and print its cost.

Input
The first line contains the single integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains the single integer n (2≤n≤105).

The second line of each test case contains n integers c1,c2,…,cn (1≤ci≤109) — the costs of each segment.

It’s guaranteed that the total sum of n doesn’t exceed 105.

Output
For each test case, print the minimum possible cost of the path from (0,0) to (n,n) consisting of at most n alternating segments.


分析

奇数位置和偶数位置分别对应着一个方向上的线段,所以两个方向可以独立开来考虑。
那么问题就变成了一个长度为 x 的线,进行分段的问题,根据贪心的想法,找到花费的最小值的位置,其他位置都只走 1 个单位的位置,其他的都在最小花费上走。
然后我们枚举每种情况,维护最小值即是答案。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int a2[100005],a1[100005],cnt2,cnt1,min1[100005],min2[100005];
ll sum1[100005],sum2[100005];
 
int main()
{
	min1[0] = min2[0] = 1e9;
	int T = 1;
	scanf("%d",&T);
	while(T--)
	{
		ll ans = 1e18;
		cnt1 = cnt2 = 0;
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			int x;
			scanf("%d",&x);
			if(i % 2 != 0) a1[++cnt1] = x;
			else a2[++cnt2] = x;
		}
		for(int i=1;i<=cnt1;i++)
		{
			sum1[i] = sum1[i - 1] + a1[i];
			min1[i] = min(min1[i - 1], a1[i]);
		}
		for(int i=1;i<=cnt2;i++)
		{
			sum2[i] = sum2[i - 1] + a2[i];
			min2[i] = min(min2[i - 1], a2[i]);
		}
		for(int i=1;i<=n/2;i++)
		{
			ans = min(ans, 1ll * (n - i) * min1[i] + sum1[i] + 1ll * (n - i) * min2[i] + sum2[i]);
		}
		for(int i=2;i<=cnt1;i++)
		{
			ans = min(ans, 1ll * (n - i) * min1[i] + sum1[i] + 1ll * (n - i + 1) * min2[i - 1] + sum2[i - 1]);
		}
		
		printf("%lld\n",ans);
	}
	return 0;
}