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;
    }
    
    
    
    
  • 相关阅读:
    OSG在VS2008下的配置安装
    MFC编译Freetype2.3.7
    在VS中编译Opencascade6.6.0
    MFC 双缓存绘图
    SDI在自定义的工具栏上添加下拉控件
    DECLARE_MESSAGE_MAP用法
    64位电脑上安装MySQL进行MFC开发的相关问题
    VS2005+VTK读入点云文件
    VS2005编译VTK5.10.1
    HTML入门:Tag学习
  • 原文地址:https://www.cnblogs.com/OvOq/p/14853215.html
Copyright © 2011-2022 走看看