zoukankan      html  css  js  c++  java
  • hust 1009 Sum the K-th

    题目描述

    N integers are arranged on a circle clockwise. Given two integers M and K. For each position, you should take M continuous integers on the left and M continuous integers on the right. Take out the K-th number of the 2M integers. Then you will get N integers. Add all the integers up and tell me the sum. If the answer is not smaller than 1,000,000,007, divide it by 1,000,000,007 and tell me the remainder.

    输入

    There is an Integer T in the first line. Which means the number of test cases in the input file. Then followed by T test cases. For each test case, in the first line there are 3 Integers N(5<=N<=100,000),M(0<M*2<N),K(0<K<2*M). Then there are N Integers, all the integers are non-negative Integers and smaller than 1,000,000,000.

    输出

    For each test case, output the answer for a line.

    样例输入

    2
    5 2 2
    1 2 3 4 5
    5 1 2
    1 2 3 4 5

    样例输出

    12
    21
    这个题一直都不明白题意,直到在某个论坛上看到这个题的题意,才明白这是一个水题,就是一个划分树的题目,简单题,具体看程序
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn=1000001;
    struct node
    {
        int num[maxn],date[maxn];
    }tree[30];
    int sorted[maxn];
    void builttree(int left,int right,int c)
    {
        //if(left==right) return;
        int mid=left+(right-left)/2;
        int same=mid-left+1;
        for (int i=left;i<=right;i++)
        {
            if(tree[c].date[i]<sorted[mid]) same--;
        }
        int ln=left,rn=mid+1;
        for(int i=left;i<=right;i++)
        {
            if(i==left)
            {
                tree[c].num[i]=0;
            }
            else
            {
                tree[c].num[i]=tree[c].num[i-1];
            }
            if(tree[c].date[i]<sorted[mid])
            {
                tree[c].num[i]++;
                tree[c+1].date[ln++]=tree[c].date[i];
            }
            else if(tree[c].date[i]>sorted[mid])
            {
                tree[c+1].date[rn++]=tree[c].date[i];
            }
            else if(tree[c].date[i]==sorted[mid])
            {
                if(same)
                {
                    same--;
                    tree[c].num[i]++;
                    tree[c+1].date[ln++]=tree[c].date[i];
                }
                else tree[c+1].date[rn++]=tree[c].date[i];
            }
        }
        if(left==right) return;
        builttree(left,mid,c+1);
        builttree(mid+1,right,c+1);
    }
    int research_tree(int a,int b,int k,int left,int right,int c)
    {
        if(left==right) return tree[c].date[left];
        int s,ss,mid=left+(right-left)/2;
        if(a==left)
        {
            s=tree[c].num[b];
            ss=0;
        }
        else
        {
            s=tree[c].num[b]-tree[c].num[a-1];
            ss=tree[c].num[a-1];
        }
        if(s>=k)
        {
            a=left+ss;
            b=left+ss+s-1;
            research_tree(a,b,k,left,mid,c+1);
        }
        else
        {
            int b2=b-a-1-s;//[a,b]中分到右孩子的个数
            int bb=a-left-ss;//表示[left,a-1]中分到右孩子的个数
            a=mid+1+a-left-ss;
            b=mid-left+1-ss+b-s;
            research_tree(a,b,k-s,mid+1,right,c+1);
        }
    }
    int main()
    {
        int n,m,x,y,k,CAS,ans;
        long long sum;
        scanf("%d",&CAS);
        while(CAS--)
        {
            scanf("%d%d%d",&n,&m,&k);
            for (int i=1;i<=n;i++)
            {
                scanf("%d",&tree[0].date[i]);
                sorted[i]=sorted[i+n]=sorted[i+n+n]=tree[0].date[i];
                tree[0].date[i+n]=tree[0].date[i+n+n]=tree[0].date[i];
            }
            sort(sorted+1,sorted+n+n+n+1);
            builttree(1,n+n+n,0);
            sum=0;
            for (int i=n+1;i<=n+n;i++)
            {
                x=i-m; y=i+m;
                ans=research_tree(x,y,k,1,n+n+n,0);
                if (ans>=tree[0].date[i]) sum+=((long long) (research_tree(x,y,k+1,1,n+n+n,0)))%1000000007;
                else sum+=((long long)(ans))%1000000007;
            }
            printf("%lld
    ",sum%1000000007);
        }
        return 0;
    }
    至少做到我努力了
  • 相关阅读:
    [SQL Server] sysobjects的介紹
    [R] [Johns Hopkins] R Programming -- week 3
    [R] 繪圖 Par 函数
    [R] [Johns Hopkins] R Programming -- week 4
    [Ms SQL] 基本創建、修改與刪除
    [R] Lexical & Dynamic Scoping / Execution & Calling environments / Closures
    [R] [Johns Hopkins] R Programming 作業 Week 2
    Python之路【第十七篇】:Django【进阶篇 】
    Django之Form组件
    Python之路【第十六篇】:Django【基础篇】
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3716815.html
Copyright © 2011-2022 走看看