cf Educational Codeforces Round 106 C. Minimum Grid Path

原题:
C. Minimum Grid Path
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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≤10^5).

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

It’s guaranteed that the total sum of n doesn’t exceed 10^5.

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.
Example
Input
Copy

3
2
13 88
3
2 3 1
5
4 3 2 1 4

Output
Copy

202
13
19

Note

In the first test case, to reach (2,2) you need to make at least one turn, so your path will consist of exactly 2 segments: one horizontal of length 2 and one vertical of length 2. The cost of the path will be equal to 2⋅c1+2⋅c2=26+176=202.

In the second test case, one of the optimal paths consists of 3 segments: the first segment of length 1, the second segment of length 3 and the third segment of length 2.

The cost of the path is 1⋅2+3⋅3+2⋅1=13.

In the third test case, one of the optimal paths consists of 4 segments: the first segment of length 1, the second one — 1, the third one — 4, the fourth one — 4. The cost of the path is 1⋅4+1⋅3+4⋅2+4⋅1=19.

中文:
给你个个大小为N×N的二维笛卡尔坐标,初始时你在(0,0)点,现在要走到(N,N)。然后给你N个数表示N个“代价”n[i],初始时你可以选择向上或者向右走,如果不转换方向,那么在该方向上走的步数要乘以n[0],第一次转换方向后走的步数乘以n[1]依次类推,最后你问你走到终点,所花费的总代价最小是多少?

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100001;
const ll inf = 9999999999999999;
int n,t;
ll c[maxn];
vector<ll> a,b;
//ofstream out("res.txt");
//ifstream in("in.txt");
int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--)
    {
        cin>>n;
        for (int i=1;i<=n;i++)
        {
            cin>>c[i];
        }
        ll ans = inf,  mi_a = inf, mi_b = inf;
        ll tmp_b = 0,tmp_a = 0, tot_a = 0, tot_b = 0, cnt_a = 0, cnt_b = 0;
        a.clear();
        b.clear();
        for (int i=1;i<=n;i++)
        {
            if (i%2)
            {
                mi_a = min(mi_a, c[i]); // 2 1
                tot_a += c[i]; // 2 1
                tmp_a = mi_a * (n - cnt_a) +  (tot_a - mi_a); // 1 * (3 - 1) + 1 * (3 - 1)
                cnt_a ++;
                a.push_back(tmp_a);
 
            }
            else
            {
                mi_b = min(mi_b, c[i]); // 3
                tot_b += c[i]; // 3
                tmp_b = mi_b * (n - cnt_b) + (tot_b - mi_b); // 3 * (3 - 0) + 0 * (3 - 3) = 9
                cnt_b ++;
                b.push_back(tmp_b);
            }
        }
/*
        for (auto x: a)
        {
            cout<<x<<" ";
        }
        cout<<endl;
 
        for (auto x: b)
        {
            cout<<x<<" ";
        }
        cout<<endl;
*/
        for (int i=0;i<a.size();i++)
        {
            if (i == 0)
            {
                ans = min(ans, a[i] + b[i]);
                continue;
            }
            if (i + 1 <= n - 1)
            {
                ans = min(ans, a[i] + b[i-1]);
            }
            if (i + 2 <= n - 1 && b.size() > i)
            {
                ans = min(ans, a[i] + b[i]);
            }
        }
 
 
        cout<<ans<<endl;
 
    }
    // in.close();
    // out.close();
    return 0;
}
 
/*
1
7
8 7 2 9 10 3 8
 
 
*/
 

思路:

假设初始时候是向右(向上也一样),那么向右走所用的代价一定是奇数下标的代价n1[i],向上的代价是偶数下标的代价n2[i],由于要求换方向的次数不超过N,这里先考虑向右走的情况,因为向右肯定是要走N步才能到终点,每次由向上的方向换到向右的方向时,判断当前所使用的代价n1[i]是不是当前便利的代价n1中最小的哪个,如果是,便用当前最小的代价走到N,之前横向行走所消耗的步数全部换成是1步,并乘以之前的代价。同理计算向上,最后得到维护的最小值即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值