Convert to Ones CodeForces(超水题)

题目大意:给你几个数,这些数里面只有0或1,你有两种操作:1.把一段区域内的所有数前后交换位置。2.把一段区域内所有数取反。(区域可大可小,可以是所有数也                       可以只有一个数)。两个操作各有它的代价,你操作一次就要消耗一次代价,求把所有数都变成1所需要的最小代价。

输入:第一行输入n , ab, c :表示有n个数,操作1代价b,操作2代价c;

      第二行输入n个数,就是你要操作的数组。

输出:一行,输出最小代价。

题目分析:

1.如何运用操作1 :

我们可以通过操作1把所有的0聚到同一个区域内,再用操作2“一杆收”。

2.如何进行比较:

①需要几次操作:原来一共有n组0。(就是几个0处于同一个区域算作一个“组”),我们可以发现:每个1操作最多会使两个“0组”合并。(一次操作 1一定可以把两个不连通的“0组”变成一个大的“0组”,可以自己摸拟试试,确实是这样)每个2操作最多能消灭一个“0组”。所以我们一共需要n次操作,因为每种操作都相当于把“0组”的个数减少了1。

②操作1最多用几次:我们需要n-1次操作1把所有0聚在一起。然后我们需要1次操作2来把一组0给转化。此时操作1使用次数最多,代价 b*(n-1)+c。

③操作2最多用几次:把所有的“0组”全部转化即可,代价c*n。

   

  我们会发现,两者都是用了n次操作,所以c和b哪个小哪个多用就可以保证最终代价变小,所以最后结果就是min(b*(n-1)+c , c*n)。换言之:c和b有大小之分,而且一共使用的次数是一定的,那么肯定让代价少的操作尽可能多使用。

上代码!

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=3e5+10;//数据范围
int a[maxn];char s[maxn];
int n,b,c;
long long cnt;
int main(){
        scanf("%d %d %d",&n,&b,&c);
        scanf("%s",s);//输入是连着的数字,用一个字符串存
        int i=0;
        while(s[i]!=‘\0‘){
            a[i+1]=s[i]-‘0‘;
            i++;
        }//把字符串转为整形数组
        for(int i=1;i<=n;i++){
            if(a[i]==0){
                while(a[i]==0){
                    i++;
                }//跳出循环时候,一个“0组”被遍历完毕。
                cnt++;
            }
        }
        //cnt表示有几个“0组”
        if(cnt==0){
            printf("0\n");
            return 0;
        }//如果这里不特判,(cnt-1)*b+c可能为负,结果就错了
        printf("%lld\n",1ll*min(cnt*c,(cnt-1)*b+c));//cnt,b,c<3e6,cnt*c可能超过int范围
    return 0;
}

原文地址:https://www.cnblogs.com/liu-yi-tong/p/12678652.html

时间: 2024-08-01 12:44:28

Convert to Ones CodeForces(超水题)的相关文章

Pearls in a Row CodeForces 620C 水题

题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意:就是给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对儿相等的数字,每个数字都属于某个部分,输出划分的部分数量以及对应区间. 很简单一道题,输入的数据都不用存的,输入一个检测一个就好了,用map打标记,用容器存一下要输出的区间端点值,最后挨个儿输出就好了 代码如下: #include<iostream> #include<map> #

Army Creation CodeForces - 813E (水题)

题意: 给定序列, 每次询问一个区间[l,r], 问[l,r]中最多能选多少个数且每种数字不超过k 相当于加强版 HH的项链, 对于一个数t, 主席树维护上k次出现的位置pre[t], 每次查询相当于求区间内pre<左端点的总数 #include <iostream> #include <queue> #define REP(i,a,n) for(int i=a;i<=n;++i) #define mid ((l+r)>>1) using namespace

codeforces Gym 100187L L. Ministry of Truth 水题

L. Ministry of Truth Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/K Description Andrey works in the Ministry of Truth. His work is changing articles in newspapers and magazines so that they praise the Party an

水题 Codeforces Round #303 (Div. 2) A. Toy Cars

题目传送门 1 /* 2 题意:5种情况对应对应第i或j辆车翻了没 3 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 4 3: if both cars turned over during the collision. 5 是指i,j两辆车,而不是全部 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath> 11 #

codeforces 505B Mr. Kitayuta&#39;s Colorful Graph(水题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge

CodeForces 22D Segments 排序水题

题目链接:点击打开链接 右端点升序,取右端点 暴力删边 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10

ytu 2558: 游起来吧!超妹!(水题,趣味数学题)

2558: 游起来吧!超妹!Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 7  Solved: 3[Submit][Status][Web Board] Description 夏天到了,无聊的超妹跑到了落雪湖里抓鱼吃.结果,游到湖的正中 央时被湖边保安看到了,保安要抓住超妹.我们假设落雪湖是一个半径为r的圆形,超妹在圆形的正中心,速度为1.保安由于不会游泳所以只能沿着湖的边缘奔 跑,速度为n.因为超妹在陆地上的速度是很快的,所以我们假设只要超妹到

水题 Codeforces Round #299 (Div. 2) A. Tavas and Nafas

题目传送门 1 /* 2 很简单的水题,晚上累了,刷刷水题开心一下:) 3 */ 4 #include <bits/stdc++.h> 5 using namespace std; 6 7 char s1[11][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven

CodeForces 707A Brain&#39;s Photos (水题)

题意:给一张照片的像素,让你来确定是黑白的还是彩色的. 析:很简单么,如果有一种颜色不是黑白灰,那么就一定是彩色的. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #i