zoukankan      html  css  js  c++  java
  • UPC Splitting Pile(前缀和+最大值的选择)

    问题 F: Splitting Pile

    题目描述
    Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer ai written on it.
    They will share these cards. First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards. Here, both Snuke and Raccoon have to take at least one card.
    Let the sum of the integers on Snuke’s cards and Raccoon’s cards be x and y, respectively. They would like to minimize |x−y|. Find the minimum possible value of |x−y|.

    Constraints
    2≤N≤2×105
    −109≤ai≤109
    ai is an integer.
    输入
    Input is given from Standard Input in the following format:
    N
    a1 a2 … aN
    输出
    Print the answer.
    样例输入 Copy
    6
    1 2 3 4 5 6
    样例输出 Copy
    1
    提示
    If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x−y|=1. This is the minimum possible value.
    题意: 给你一个序列,让你把序列分为两部分,使得两部分的和的差值最小。
    解题思路: 维护一个前缀和就可以了~首先要计算出总和,总和减去前缀和就是剩余序列的和,再减去一次前缀和就是所需答案。然后找出最小值就可。
    注意: 看一眼数据范围的话,初始化最小值要为long long 的最大值,不然有一个测试点卡不过去qwq 也可能是我太弱了(暴风哭泣)
    我以前一直以为inf是int型最大值(手动滑稽)

     1. 0x7fffffff :int的最大值, 0x7 后跟7个f,memset (num,0x7f,sizeof(num));
     2. 0x3f3f3f3f, 1061109567, 0x后跟4个3f, memset(num,0x3f,sizeof(num))
    

    代码:

    #include<bits/stdc++.h>
    const int inf=0x3f3f3f3f;
    #define cutele main
    using namespace std;
    typedef long long ll;
    const int N=1e6+7;
    ll n,a[N],sum[N],s;
    void solve(){
       ll minn=0x7fffffff;
       for(ll i=1;i<n;i++)
            minn=min(minn,abs(s-sum[i]*2));
       cout<<minn<<endl;
    }
    //前缀和
    int cutele(){
        cin>>n;
        for(ll i=1;i<=n;i++){
            cin>>a[i];
            sum[i]=sum[i-1]+a[i];
            s+=a[i];
        }
        solve();
        return 0;
    }
    
    
    
    
  • 相关阅读:
    ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
    pycocotools使用教程
    with torch.no_grad() 详解
    虚拟机Ubuntu上下载Pytorch显示超时
    Deep Layer Aggregation DLA网络的结构
    tgz文件解压命令
    install mysql at linux
    devops issue
    process data
    unittest
  • 原文地址:https://www.cnblogs.com/OvOq/p/14853215.html
Copyright © 2011-2022 走看看