首页 > 其他分享 >E. Arranging The Sheep

E. Arranging The Sheep

时间:2024-02-28 21:14:20浏览次数:25  
标签:Sheep right cur point int long Arranging left

This is a programing problem on codefores with a difficulty score of 1400.
It presents an intresting challenge that can be solved using the principle of greediness.
Initially, it's evident that we need to move each shape one by one and gather them with leaving any empty space between them.
Finding a bruteForce solution seems challenging.Perhaps, we might consider trying all possible segments along the line to find the answer....However, since the length of the segment is constant, we only need to try at most n segments.Then, by moving every element into the segment, we arrive at an O(n²) solution.

Returning the main point, assuming we already find the segment, there must be one point where all shapes to the left of this point are aligned to the left, and similarly, shapes to the righg are aligned to the right.Our task is to find this point.

https://codeforces.com/problemset/problem/1520/E

void solve(){
    int n;
    cin >> n;

    string s;
    cin >> s;

    vector<long long> left(n + 1);
    vector<long long> right(n + 1);

    for (int i = 0, cur = 0; i < n; ++i){
        left[i + 1] = left[i];
        if (s[i] == '.'){
            left[i + 1] += cur;
        }
        else{
            cur ++;
        }
    }

    for (int i = n - 1, cur = 0; i >= 0; --i){
        right[i] = right[i + 1];
        if (s[i] == '.'){
            right[i] += cur;
        }
        else{
            cur ++;
        }
    }

    long long ans = (long long)1e14;
    for (int i = 1; i <= n; ++i){
        ans = min(ans, left[i] + right[i - 1]);
    }

    cout << ans << '\n';
}

标签:Sheep,right,cur,point,int,long,Arranging,left
From: https://www.cnblogs.com/yxcblogs/p/18041824

相关文章

  • AtCoder Grand Contest 010 E Rearranging
    洛谷传送门AtCoder传送门赛时在想一些奇怪的东西,没想到建图。考虑使用元素两两之间的相对顺序来描述序列。发现若\(x,y\)互质那么它们的相对顺序被确定了。先把输入的序列从小到大排序。然后考虑互质的数之间先连一条无向边。那么先手要把无向边定向使得它是个DAG,后手会......
  • [AGC010E] Rearranging
    Thereare$N$integerswrittenonablackboard.The$i$-thintegeris$A_i$.TakahashiandAokiwillarrangetheseintegersinarow,asfollows:First,Takahashiwillarrangetheintegersashewishes.Then,Aokiwillrepeatedlyswaptwoadjacentintege......
  • [JOI 2023 Final] Stone Arranging 2
    洛谷P9349题意一种区间覆盖操作,可以考虑直接无脑线段树,复杂度为\(O(nlog_n)\)。但是观察后发现可以开一个桶,记录这个数在序列中出现的最后一次的下标。循环扫一遍原序列,从小到大对于每一个a[i],使得下标i到m[a[i]]的区间全部覆盖为a[i]。每次覆盖一个小区间后,因为前面的区间已......
  • [ABC317G] Rearranging
    ProblemStatementThereisagridwith$N$rowsand$M$columns.Thesquareatthe$i$-throwfromthetopandthe$j$-thcolumnfromtheleftcontainstheinteger$A_{i,j}$.Here,thesquarescontain$M$occurrencesofeachof$1,\ldots,N$,foratotalo......
  • [ABC317G] Rearranging 题解
    取自我的洛谷博客:https://www.luogu.com.cn/blog/SunnyYuan/solution-at-abc317-g借鉴了官方题解思路。思路首先我们要建立一个二分图。对于输入的\(a_{i,j}\),我们可以连接左侧的\(i\)和右侧的\(a_{i,j}\)。比如样例\(1\):注意:左边的\(1,2,3\)和右边的\(1,2......
  • 羊 老虎 饲养员 animal=random.choice([Tiger,Sheep]) 该animal类型是对象
    #羊老虎饲养员importrandom#基类classAnimal():#属性def__init__(self,animal,w,call,food,room_num):self._animal=animalself._w=wself._call=callself._food=foodself._room_num=room_num#动作......
  • AtCoder Beginner Contest 247 Ex Rearranging Problem
    洛谷传送门AtCoder传送门考虑我们如何判定一个排列是否能成为最终答案。连边\(i\top_i\),设环数为\(k\),那么最少交换次数为\(n-k\)。那么充要条件是,每个环所有点的\(c_i\)相同,并且\(n-k\leK\)且\(2\mid(K-(n-k))\)。\(K\)和\(n-k\)奇偶性相同是因为,......
  • 1846.maximum element after decreasing and rearranging 减小和重新排列数组后的最大
    问题描述1846.减小和重新排列数组后的最大元素解题思路由于题目允许我们重新排列数组中的元素任意次,因此首先将数组排序,根据arr中第一个元素必须为1,以及相邻两元素的差......
  • 高性能 Python web 框架 Blacksheep 初见
    Pythonweb框架性能对比一说到Python大家多半最先想到的就是它代码的简洁与性能的孱弱。在我所使用体验过的Pythonweb框架中Tornado性能最好,Flask次之,Django最差......
  • sheep match disappear game All In One
    sheepmatchdisappeargameAllInOne羊了个羊小游戏在线网页版sheepNsheepH5gamedogmatchdisappeargameAllInOne狗了个狗小游戏在线网页版dogNdogH5......