zoukankan      html  css  js  c++  java
  • String of Colorful Beads

    Eva would like to buy a string of beads with no repeated colors so she went to a small shop of which the owner had a very long string of beads. However the owner would only like to cut one piece at a time for his customer. With as many as ten thousand beads in the string, Eva needs your help to tell her how to obtain the longest piece of string that contains beads with all different colors. And more, each kind of these beads has a different value. If there are more than one way to get the longest piece, Eva would like to take the most valuable one. It is guaranteed that such a solution is unique.

    Input Specification:

    Each input file contains one test case. Each case first gives in a line a positive integer N (≤ 10,000) which is the length of the original string in the shop. Then N positive numbers (≤ 100) are given in the next line, which are the values of the beads -- here we assume that the types of the beads are numbered from 1 to N, and the i-th number is the value of the i-th type of beads. Finally the last line describes the original string by giving the types of the beads from left to right. All the numbers in a line are separated by spaces.

    Output Specification:

    For each test case, print in a line the total value of the piece of string that Eva wants, together with the beginning and the ending indices of the piece (start from 0). All the numbers must be separated by a space and there must be no extra spaces at the beginning or the end of the line.

    Sample Input:

    8
    18 20 2 97 23 12 8 5
    3 3 5 8 1 5 2 1
    
     

    Sample Output:

    66 3 6
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main()
     4 {
     5 //    freopen("data.txt","r",stdin);
     6     int n,k;
     7     scanf("%d",&n);
     8     vector<int> v,vl;
     9     vector<int> vi(n+1,-1);
    10     for(int i=0;i<n;i++)
    11     {
    12         scanf("%d",&k);
    13         v.emplace_back(k);
    14     }
    15     int vls=0,b=-1,e=-1,s=0,mb=0,ms=0;
    16     for(int i=0;i<n;i++)
    17     {
    18         scanf("%d",&k);
    19         vls+=v[k-1];
    20         vl.emplace_back(vls);
    21         if(vi[k]>=mb)
    22         {
    23             if(s<ms)
    24             {
    25                 b=mb;
    26                 e=i-1;
    27                 s=ms;
    28             }
    29             ms=vls-vl[vi[k]];
    30             mb=vi[k]+1;
    31         }
    32         else
    33         ms+=v[k-1];
    34         vi[k]=i;
    35     }
    36     if(s<ms)
    37     {
    38         b=mb;
    39         e=n-1;
    40         s=ms;
    41     }
    42     printf("%d %d %d",s,b,e);
    43     return 0;
    44 }
  • 相关阅读:
    Spring 源码解析之 XML 解析 到 BeanDefinition
    Mybatis 流程解析 之 mapper映射
    Mybatis 流程解析 之 配置加载
    (二) mybatis 源码分析之日志
    CNC 1320
    CNC 1815
    linux中jdk安装,配置环境变量
    ssm配置文件,方便以后使用
    01 标识符,基本数据类型,自动类型转换与强制类型转换,自增与自减,三元运算符,键盘录入
    N46期第二十一周作业
  • 原文地址:https://www.cnblogs.com/SkystarX/p/12285756.html
Copyright © 2011-2022 走看看