zoukankan      html  css  js  c++  java
  • HDU 2217 Data Structure?

    C - Data Structure?
    Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently. Today let me introduce a data-structure-like problem for you. 
    Original, there are N numbers, namely 1, 2, 3...N. Each round, iSea find out the Ki-th smallest number and take it away, your task is reporting him the total sum of the numbers he has taken away. 
     

    Input

    The first line contains a single integer T, indicating the number of test cases. 
    Each test case includes two integers N, K, K indicates the round numbers. Then a line with K numbers following, indicating in i (1-based) round, iSea take away the Ki-th smallest away. 

    Technical Specification
    1. 1 <= T <= 128 
    2. 1 <= K <= N <= 262 144 
    3. 1 <= Ki <= N - i + 1 
     

    Output

    For each test case, output the case number first, then the sum.
     

    Sample Input

    2 3 2 1 1 10 3 3 9 1
     

    Sample Output

    Case 1: 3 Case 2: 14
     
    这道题目能够用线段树,或者是树状数组来解决
    对于题意而言,他的意思是给你n个数字,分别为1...n然后是
    给你询问,让你去除这个数,可是询问的内容是序列,比方说1,2,3假设我取出了2,那么当询问为2的时候就是3了。由于序列变为了1,3

    然后是通过线段树的标记功能

    /*
    Problem : 4217 ( Data Structure?

    ) Judge Status : Accepted RunId : 13881893 Language : C++ Author : 24862486 Timer : 998ms Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define lson rt << 1 , L , mid #define rson rt << 1 | 1 , mid + 1 , R const int maxn=262144+5; int n,k,ki,T; int sum[maxn<<2]; void pushup(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void build(int rt,int L,int R) { if(L==R) { sum[rt]=1; return; } int mid=(L+R)>>1; build(lson); build(rson); pushup(rt); } int query(int p,int rt,int L,int R) { if(L==R) { sum[rt]=0; return R; } int mid=(L+R)>>1; int res; if(sum[rt<<1]>=p)res=query(p,lson);//向终点靠拢 else res=query(p-sum[rt<<1],rson); pushup(rt); return res; } int main() { scanf("%d",&T); for(int t=1; t<=T; t++) { scanf("%d%d",&n,&k); build(1,1,n); long long res=0; for(int i=0; i<k; i++) { scanf("%d",&ki); res+=query(ki,1,1,n); } printf("Case %d: %lld ",t,res); } return 0; }



  • 相关阅读:
    [leetcode]Evaluate Division
    [leetcode]Read N Characters Given Read4 II
    [leetcode]Shortest Palindrome
    vim基础
    mac 默认设置python3最新版本环境变量
    mac 如何获取最高权限(关闭安全保护机制)
    mac 终端成功执行scrapy命令
    解决虚拟机VMware下ubuntu16.04LTS打不开软件中心Ubuntu Software
    解决虚拟机VMware下ubuntu16.04LTS异常连不上网
    SOA 服务架构之简介及理解
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5370565.html
Copyright © 2011-2022 走看看