zoukankan      html  css  js  c++  java
  • Gym

    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 <stdio.h>
     3 #define ll long long
     4 #define INF 0x3f3f3f3f
     5 using namespace std;
     6 const int N = 1e6+10;
     7 int a[N], b[N/10+10][5];
     8 int main() {
     9     int n;
    10     cin >> n;
    11     for(int i = 1; i <= n; i ++){
    12         scanf("%d", &a[i]);
    13         if(!b[a[i]][0]) b[a[i]][1] = b[a[i]][2] = b[a[i]][3] = i;
    14         else {
    15             b[a[i]][2] = b[a[i]][3];
    16             b[a[i]][3] = i;
    17             b[a[i]][4] = max(b[a[i]][4], b[a[i]][3] - b[a[i]][2] - 1);            
    18         } 
    19         b[a[i]][0] ++;
    20     }
    21     int ans = INF;
    22     for(int i = 1; i <= n; i ++) {
    23         b[a[i]][4] = max(b[a[i]][4], n - b[a[i]][3] + b[a[i]][1] - 1);
    24         ans = min(ans, b[a[i]][4] + n);
    25     }
    26     printf("%d
    ",ans);
    27     return 0;
    28 }
  • 相关阅读:
    继续Delphi调用Wcf
    我用 Windows Live Writer 写随笔
    "WCF 服务编程"刚到,第一印象,内纸张很差
    我的asp.net网站小项目,体现了我学习的几个阶段,现在学习到WCF阶段
    菜单设计
    求圆的面积
    dataGridView 批量更新
    Android简单实现对话框
    dephi 程序输入法中英文自动切换实现
    Delphi捕捉DLL执行所抛出的异常。(转)
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7616563.html
Copyright © 2011-2022 走看看