zoukankan      html  css  js  c++  java
  • B. Shift and Push

    B. Shift and Push
    time limit per test
    1.0 s
    memory limit per test
    256 MB
    input
    standard input
    output
    standard output

    Given two equally sized arrays A and B of size N. A is empty and B has some values.

    You need to fill A with an element X such that X belongs to B.

    The only operations allowed are:

    1. Copy B[i] to A[i].

    2. Cyclic shift B by 1 to the the right.

    You need to minimise the number of operations.

    Input

    The first line contains a single positive integer N(1 ≤ N ≤ 106), denoting the size of the arrays.

    Next line contains N space separated positive integers denoting the elements of the array B(1 ≤ B[i] ≤ 105).

    Output

    Output a single integer, denoting the minimum number of operations required.

    Examples
    Input
    3
    1 2 3
    Output
    5
    Input
    6
    1 1 2 2 3 3
    Output
    10
    Note

    In the first test case:

    We can have 5 steps as: fill first element, shift, fill second element, shift, fill third element.

    Initially, A = [_, _, _], B = [1, 2, 3]

    After step 1, A = [1, _, _], B = [1, 2, 3]

    After step 2, A = [1, _, _], B = [3, 1, 2]

    After step 3, A = [1, 1, _], B = [3, 1, 2]

    After step 4, A = [1, 1, _], B = [2, 3, 1]

    After step 5, A = [1, 1, 1], B = [2, 3, 1]

    求每个数中相邻区间最大的最小的那个数。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <map>
     6 #include <set>
     7 #include <queue>
     8 #include <stack>
     9 #include <vector>
    10 #define ll long long int
    11 #define inf 0x3f3f3f3f
    12 #define N 1000005
    13 #define mem(a) memset(a,0,sizeof(a))
    14 using namespace std;
    15 int lowbit(int x){ return x&(-x);}
    16 int a[N],b[N/10+5][5];
    17 
    18 int main(){
    19   int n;
    20   cin>>n;
    21   for(int i = 1; i <= n; i ++){
    22       scanf("%d", &a[i]);
    23       if(!b[a[i]][0]) b[a[i]][1] = b[a[i]][2] = b[a[i]][3] = i;
    24       else {
    25           b[a[i]][2] = b[a[i]][3];
    26           b[a[i]][3] = i;
    27           b[a[i]][4] = max(b[a[i]][4], b[a[i]][3] - b[a[i]][2] - 1);
    28       }
    29       b[a[i]][0] ++;
    30   }
    31   int ans = inf;
    32   for(int i = 1; i <= n; i ++) {
    33       b[a[i]][4] = max(b[a[i]][4], n - b[a[i]][3] + b[a[i]][1] - 1);
    34       ans = min(ans, b[a[i]][4] + n);
    35   }
    36   printf("%d
    ",ans);
    37   return 0;
    38 }
  • 相关阅读:
    CPU和GPU实现julia
    基于SURF特征的图像与视频拼接技术的研究和实现(一)
    验证码识别--type7
    机器视觉项目基础框架
    实现gabor filter的滤波
    神经网络研究项目--以工程师的视角
    集装箱项目
    基于海康监控的图像识别设计
    反人脸识别的思路和实现
    单向信息传输系统设计实现
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/7618280.html
Copyright © 2011-2022 走看看