Into Blocks (easy version)
G1 - Into Blocks (easy version)
思路:先把数据预处理一遍,找到每一种数的最右端的位置,和每一种数的出现的次数,然后,从第一个数字开始遍历,用
r保存当前这一块的最大右端,用MAX来记录当前块的出现数字的最大出现次数(不一定跟最大右端的数字对应相同),而当遍历到当前的最大右端时,则将当前块的所有数字变为出现次数最多的数字,那么此时所进行的操作就是最少的。这里利用的是贪心的思想。
代码:
// Created by CAD on 2019/9/21.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+5;
int a[maxn],cnt[maxn],maxr[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,q;
cin>>n>>q;
for(int i=1;i<=n;++i)
cin>>a[i],cnt[a[i]]++,maxr[a[i]]=i;
int r=0,MAX=0,l=1;
ll ans=0;
for(int i=1;i<=n;++i)
{
r=max(r,maxr[a[i]]);
MAX=max(MAX,cnt[a[i]]);
if(i==r)
ans+=r-l+1-MAX,l=r+1,MAX=r=0;
}
cout<<ans<<endl;
return 0;
}
Into Blocks (easy version)的更多相关文章
- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2) G1. Into Blocks (easy version)
题目:https://codeforc.es/contest/1209/problem/G1 题意:给你一个序列,要你进行一些操作后把他变成一个好序列,好序列的定义是,两个相同的数中间的数都要与他相同 ...
- Numerical Sequence (easy version)
http://codeforces.com/problemset/problem/1216/E1 E1. Numerical Sequence (easy version) time limit pe ...
- Ping-Pong (Easy Version)(DFS)
B. Ping-Pong (Easy Version) time limit per test 2 seconds memory limit per test 256 megabytes input ...
- ZOJ 3868 - Earthstone: Easy Version
3868 - Earthstone: Easy Version Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld ...
- Codeforces 1077F1 Pictures with Kittens (easy version)(DP)
题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...
- UVA12569-Planning mobile robot on Tree (EASY Version)(BFS+状态压缩)
Problem UVA12569-Planning mobile robot on Tree (EASY Version) Accept:138 Submit:686 Time Limit: 300 ...
- Coffee and Coursework (Easy version)
Coffee and Coursework (Easy version) time limit per test 1 second memory limit per test 256 megabyte ...
- 2016级算法第六次上机-B.ModricWang's FFT : EASY VERSION
1114 ModricWang's FFT EASY VERSION 思路 利用FFT做大整数乘法,实际上是把大整数变成多项式,然后做多项式乘法. 例如,对于\(1234\),改写成\(f(x)=1* ...
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
随机推荐
- Datetime 在C#中的用法 获取当前时间的各种格式
DateTime 获得当前系统时间: DateTime dt = DateTime.Now; Environment.TickCount可以得到“系统启动到现在”的毫秒值 DateTime now = ...
- ModbusRtu通信报文详解【一】
Modbus协议可谓是工业控制领域应用最广泛的协议之一.根据不同的电气接口,包括Modbus Rtu/ASCII,Modbus TCP/UDP,从学习的角度来说,只要学会其中一种,剩余的都是大同小异的 ...
- $.ajax通用格式&&XMLHttpRequest对象属性和方法
$.ajax({ url: "", type: "POST", async: false, cache:false, //默认true data: {}, da ...
- wpf GeometryDrawing 绘制文字
<GeometryDrawing x:Key="GeometryDrawingText"> <GeometryDrawing.Geometry> <R ...
- apply()和call()的方法
apply()和call()的方法的区别 参考文档https://www.cnblogs.com/lengyuehuahun/p/5643625.html 一直都没太明白apply()与call()的 ...
- Plugin 表格列自定义显示隐藏插件TableCustom.js
TableCustom 案例展示 下载地址 https://github.com/chaorenzeng/TableCustom.js.git 快速使用 1.引入TableCustom.css和Tab ...
- java学习之—二叉树
package com.data.java.towtree; import java.io.IOException; /** * 二叉树 * @Title: uminton */ class Node ...
- Samba编码设置方法
弟管理學校的網頁伺服器,該伺服器也同時是大家的分享檔案集散中心,是以Linux架設起來的,該伺服器以 Unicode 作為系統編碼,而其他Windows系統則是big5(MS950)編碼,最近我要讓另 ...
- kafka api的基本使用
kafka API kafka Consumer提供两套Java API:高级Consumer API.和低级Consumer API. 高级Consumer API 优点: 高级API写起来简单,易 ...
- python zip用法
import requests url = "https://magi.com/search" querystring = {"q":"堕却乡&quo ...