• LightOJ


    链接:

    https://vjudge.net/problem/LightOJ-1369

    题意:

    The problem you need to solve here is pretty simple. You are give a function f(A, n), where A is an array of integers and n is the number of elements in the array. f(A, n) is defined as follows:

    long long f( int A[], int n ) { // n = size of A

    long long sum = 0;
    
    for( int i = 0; i < n; i++ )
    
        for( int j = i + 1; j < n; j++ )
    
            sum += A[i] - A[j];
    
    return sum;
    

    }

    Given the array A and an integer n, and some queries of the form:

    1.  0 x v (0 ≤ x < n, 0 ≤ v ≤ 106), meaning that you have to change the value of A[x] to v.
      
    2.  1, meaning that you have to find f as described above.
      

    思路:

    找规律,计算每个位置的贡献。
    a[i]的贡献 = (n-1-i)a[i]-ia[i];

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<math.h>
    #include<vector>
    #include<map>
    
    using namespace std;
    typedef long long LL;
    const int INF = 1e9;
    
    const int MAXN = 1e5+10;
    const int MOD = 1e9+7;
    
    LL A[MAXN];
    int n, q;
    
    LL f(LL A[], int n)
    {
        LL sum = 0;
        for (int i = 0;i < n;i++)
        {
            for (int j = i+1;j < n;j++)
                sum += A[i]-A[j];
        }
        return sum;
    }
    
    int main()
    {
        int t, cnt = 0;
        scanf("%d", &t);
        while(t--)
        {
            printf("Case %d:", ++cnt);
            scanf("%d%d", &n, &q);
            for (int i = 0;i < n;i++)
                scanf("%lld", &A[i]);
            LL sum = 0;
            for (int i = 0;i < n;i++)
            {
                sum += (n-1-i)*A[i];
                sum -= i*A[i];
            }
            int op, x, v;
            puts("");
            while(q--)
            {
                scanf("%d", &op);
                if (op == 0)
                {
                    scanf("%d%d", &x, &v);
                    sum -= (n-1-x)*A[x];
                    sum += x*A[x];
                    A[x] = v;
                    sum += (n-1-x)*A[x];
                    sum -= x*A[x];
                }
                else
                {
                    printf("%lld
    ", sum);
                }
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    js 字符串转化成数字
    SDK编程之多线程编程
    C/C++内存泄漏及检测
    那些争议最大的编程观点(转)
    DB2日常维护——REORG TABLE命令优化数据库性能(转)
    ireport报表学习
    自定义hexo的某个主题
    mac下搭建码云gitee+hexo博客
    python日期及时间格式转换
    python获取中文首字母
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11841948.html
Copyright © 2020-2023  润新知