Educational Codeforces Round 106 (Rated for Div. 2)——C题Minimum Grid Path

该博客详细解析了Codeforces竞赛1499C题目的解题思路。题目涉及将奇数位长度和偶数位长度的数分别相加等于n的情况,目标是最小化特定权重序列的和。博主通过分析得出结论,对于每个位置的权重,除最小值外赋予1,最小值赋予剩余和,可以确保总和最小。然后博主给出代码实现,通过从前往后枚举维护最小值来求解问题。
摘要由CSDN通过智能技术生成
题目链接:https://codeforces.com/contest/1499/problem/C

题解

感觉有点假的一道题,尤其是那个假的结论。
不难发现我们可以把问题转换成下面这个问题。
首先我们知道
l e n g t h 1 + l e n g t h 3 + l e n g t h 5 + . . . = n length_{1}+length_{3}+length_{5}+...=n length1+length3+length5+...=n
l e n g t h 2 + l e n g t h 4 + l e n g t h 6 + . . . = n length_{2}+length_{4}+length_{6}+...=n length2+length4+length6+...=n
现在让你最小化 ∑ i = 1 k c i ∗ l e n g t h i \sum^{k}_{i=1}c_{i}*length_{i} i=1kcilengthi
即最小化
c 1 ∗ l e n g t h 1 + c 3 ∗ l e n g t h 3 + c 5 l e n g t h 5 + . . . c_{1}*length_{1}+c_{3}*length_{3}+c_{5}length_{5}+... c1length1+c3length3+c5length5+...
c 2 ∗ l e n g t h 2 + c 4 ∗ l e n g t h 4 + c 6 l e n g t h 6 + . . . c_{2}*length_{2}+c_{4}*length_{4}+c_{6}length_{6}+... c2length2+c4length4+c6length6+...
结论就是对除最小值 c i c_{i} ci的其他数赋值为 1 1 1,对最小值赋值为剩余的数,这样即可保证最终的和最小,结论有点假。
知道这个结论后就好办了,把上面的结论公式化之后就是 s u m O d d + m i n O d d ⋅ ( n − c n t O d d ) + s u m E v e n + m i n E v e n ⋅ ( n − c n t E v e n ) sumOdd + minOdd \cdot (n - cntOdd)+sumEven + minEven \cdot (n - cntEven) sumOdd+minOdd(ncntOdd)+sumEven+minEven(ncntEven)
因为我们取要求的是必须从前往后取,那么我们直接从前往后枚举维护答案即可。

代码

#include <bits/stdc++.h>
#define PI atan(1.0)*4
#define rp(i,s,t) for (register int i = (s); i <= (t); i++)
#define RP(i,t,s) for (register int i = (t); i >= (s); i--)
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define ll long long
#define ull unsigned long long
#define mst(a,b) memset(a,b,sizeof(a))
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define m_p make_pair
#define p_b push_back
#define ins insert
#define era erase
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define dg if(debug)
#define pY puts("YES")
#define pN puts("NO")
#define outval(a) cout << "Debuging...|" << #a << ": " << a << "\n";
#define outval2(a,b) cout << "Debuging...|" << #a << ": " << a <<"\t"<< #b << ": " << b << "\n";
#define outval3(a,b,c) cout << "Debuging...|" << #a << ": " << a <<"\t"<< #b << ": " << b <<"\t"<< #c << ": " << c << "\n";
using namespace std;
int debug = 0;
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
inline int read(){
    int s=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=s*10+ch-'0';
        ch=getchar();
    }
    return s*f;
}
const int N = 2e5+7;
int c[N];
ll sum[N];
void solve(){
    int n=read();
    rp(i,1,n) c[i]=read(),sum[i]=sum[i-1]+c[i];
    int MIN1=c[1];
    int MIN2=c[2];
    ll ans=1ll*n*(MIN1+MIN2);
    int cnt1=1;
    int cnt2=1;
    rp(i,3,n){
        if(i&1) MIN1=min(MIN1,c[i]),cnt1++;
        else MIN2=min(MIN2,c[i]),cnt2++;
        ans=min(ans,sum[i]+1ll*(n-cnt1)*MIN1+1ll*(n-cnt2)*MIN2);
    }
    cout<<ans<<endl;
}
int main(){
    //ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    //debug = 1;
#endif
    //time_t beg, end;
    //if(debug) beg = clock();

    int T=read();
    while(T--) solve();

    /*
    if(debug) {
        end = clock();
        printf("time:%.2fs\n", 1.0 * (end - beg) / CLOCKS_PER_SEC);
    }
    */
    return 0;
}
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问组成,选手需要根据给定的问描述和测试用例,编写程序来解决这些问。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值