zoukankan      html  css  js  c++  java
  • NEFU 698 Post office 大概是dp?

    Post office

    Time Limit 1000ms

    Memory Limit 65536K

    description

    There are N(N<=1000) villages along a straight road, numbered from 1 to N for simplicity. We know exactly the position of every one (noted pos[i],pos[i] is positive integer and pos[i]<=10^8). The local authority wants to build a post office for the people living in the range i to j(inclusive). He wants to make the sum of |pos[k]-position_of_postoffice| (i<=k<=j) is minimum. 
    							

    input

      For each test case, the first line is n. Then n integer, representing the position of every village and in acending order. Then a integer q (q<=200000), representing the queries. Following q lines, every line consists of two integers i and j. the input file is end with EOF. Total number of test case is no more than 10.
      Be careful, the position of two villages may be the same.
    							

    output

      For every query of each test case, you tell the minimum sum.
    							

    sample_input

    3
    1 2 3
    2
    1 3
    2 3
    0
    							

    sample_output

    2
    1
    							

    hint

      Huge input,"scanf" is recommend.
    ------------------

    可以用dp进行一下预处理=。=

    但是!!!!暴力也能过!!!!!

    ------------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    long long a[1111];
    long long f[1111];
    long long g[1111];
    int n;
    int q;
    
    
    int main()
    {
        while (~scanf("%d",&n))
        {
            if (n==0) break;
            memset(a,0,sizeof(a));
            memset(f,0,sizeof(f));
            memset(g,0,sizeof(g));
    
            for (int i=1;i<=n;i++)
            {
                scanf("%I64d",&a[i]);
                f[i]=f[i-1]+a[i];
            }
            for (int i=n;i>=1;i--)
            {
                g[i]=g[i+1]+a[i];
            }
            scanf("%d",&q);
            while (q--)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                int mid=(x+y)/2;
                long long ans=(a[mid]*(mid-x)-(f[mid-1]-f[x-1]))+((g[mid+1]-g[y+1])-a[mid]*(y-mid));
                printf("%I64d\n",ans);
            }
        }
        return 0;
    }
    





  • 相关阅读:
    导出数据到Excel
    VB中导入数据到Excel内容换行
    上传附件导入,确认弹框取消后,无法上传附件
    扫描审核,出错声音报警
    C语言中,隐藏结构体的细节
    C语言 符号的不同意义
    IAR 和 keil 使用外部 SDRAM 的区别
    C语言 ringBuffer 实现
    C语言 malloc 内存泄漏
    STM32 硬件CRC和软件CRC速度比较
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226385.html
Copyright © 2011-2022 走看看