ZOJ 3790 Consecutive Blocks

题目链接~~>

做题感悟:这题可以直接二分,也可以分段二分和枚举差不多。

解题思路:

存每个数的时候同时存每个数的下标,然后排个序先按从小到大排,再按下标从小到大排,这样相同的数都在一块,因为最多可以选 k 个,那么我们就按 K 个来,这样依次枚举每个数的最大范围(在同一个数的区间内),分段二分就可以了。感觉数据有点水,写完竟然 1 A .

代码:

#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std  ;
#define INT long long int
#define L(x)  (x * 2)
#define R(x)  (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.0000000001 ;
const double PI = acos(-1.0) ;
const int mod = 1000000007 ;
const int MY = (1<<5) + 5 ;
const int MX = (1<<20) + 5 ;
const int S = 20 ;
int n ,m ;
int sum[MX] ,g[MX] ;
struct node
{
    int c ,id ;
}T[MX] ;
bool cmp(node a ,node b)
{
    if(a.c == b.c)  return a.id < b.id ;
    else   return a.c < b.c ;
}
int main()
{
    while(~scanf("%d%d" ,&n ,&m))
    {
        for(int i = 1 ;i <= n ; ++i)
        {
            scanf("%d" ,&T[i].c) ;
            T[i].id = i ;       // 位置
        }
        sort(T ,T+n+1 ,cmp) ;
        memset(sum ,0 ,sizeof(sum)) ;
        g[0] = 0 ; g[1] = T[1].c ;
        for(int i = 2 ;i <= n ; ++i)
        {
            if(T[i].c == T[i-1].c)
                 sum[i] = sum[i-1] + T[i].id - T[i-1].id -1 ;
            g[i] = T[i].c ;
        }
        int i = 1 ,ans = 1 ,Le ,Rt ;
        while(i < n)
        {
            Le = i ;
            Rt = upper_bound(g ,g+n+1 ,T[i].c) - g ; // 寻找同一组的边界
            for(int j = Le ;j < Rt ; ++j)
            {
                int mx = upper_bound(sum+Le ,sum+Rt ,sum[j]+m) - sum ;
                ans = max(ans ,mx - j) ;
            }
            i = Rt ;
        }
        cout<<ans<<endl ;
    }
    return 0 ;
}
时间: 2024-10-25 07:13:34

ZOJ 3790 Consecutive Blocks的相关文章

zoj 3790 Consecutive Blocks(链表重点是思想)

Consecutive Blocks Time Limit: 2 Seconds      Memory Limit: 65536 KB There are N (1 ≤ N ≤ 105) colored blocks (numbered 1 to N from left to right) which are lined up in a row. And the i-th block's color is Ci (1 ≤ Ci ≤ 109). Now you can remove at mos

2014 Super Training #8 B Consecutive Blocks --排序+贪心

当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就是滑动窗口的思想. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algor

ZOJ Monthly, June 2014 解题报告

A.Another Recurrence Sequence B.Gears 题目大意:有n个齿轮,一开始各自为一组,之后进行m次操作,包括以下4种类型: 1.合并两组齿轮,合并的两个应该反向旋转 2.把某个齿轮从所在组删除,自为一组,但不影响同组其它齿轮的状态与关系 3.询问两个齿轮是同向.反向或无关系(即不在同一组) 4.询问某个齿轮所在组的齿轮总数 分析:典型的并查集操作,但是注意两点: 1.由于操作3要询问两个齿轮的相对状态,因此对并查集中每个元素应当保存它的状态信息.状态是相对的,只需要

ZOJ Monthly, June 2014 月赛BCDEFGH题题解

比赛链接:点击打开链接 上来先搞了f.c,,然后发现状态不正确,一下午都是脑洞大开,, 无脑wa,无脑ce...一样的错犯2次.. 硬着头皮搞了几发,最后20分钟码了一下G,不知道为什么把1直接当成不能加油的站就会wa..太弱.. 唔···太懒第二天才发题解.. B:Gears 并查集 题解:点击打开链接 C:Consecutive Blocks 离散化一下然后模拟 题解:点击打开链接 D:An Easy Game 设dp[i][j]为前i个位置已经匹配了j个位置的方法数. #include <

File System Design Case Studies

SRC=http://www.cs.rutgers.edu/~pxk/416/notes/13-fs-studies.html Paul Krzyzanowski April 24, 2014 Introduction We've studied various approaches to file system design. Now we'll look at some real file systems to explore the approaches that were taken i

Attempting to track I/O with systemtap

https://glandium.org/blog/?p=1476 Attempting to track I/O with systemtap There are several ways a program can hit the disk, and it can be hard to know exactly what’s going on, especially when you want to take into account the kernel caches. This incl

HBase HFileBlock

HFileBlock官方源码注释: Reading HFile version 1 and 2 blocks, and writing version 2 blocks. In version 1 all blocks are always compressed or uncompressed, as specified by the HFile's compression algorithm, with a type-specific magic record stored in the be

CodeForces250B——Restoring IPv6(字符串处理)

Restoring IPv6 DescriptionAn IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons — 8 blocks in total, each block has four hexadecimal digits. Here i

CodeForces 250B Restoring IPv6 解题报告

Description An IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons — 8 blocks in total, each block has four hexadecimal digits. Here is an example o