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;
    }
    
    
    
    
  • 相关阅读:
    JDBC遇到向ORACLE数据库表执行插入操作时,报错“列在此处不允许”
    关于对称加密和非对称加密以及签名,认证和证书的理解
    .net framework 各版本区别
    数据库设计三大范式
    业务系统设计
    修改 Windows 服务器默认远程端口3389
    iis读取不到本地证书问题 提示已经导入成功
    HTTPS 建立连接的详细过程
    使用ServiceStack构建Web服务
    转-微信支付(公众号支付JSAPI)
  • 原文地址:https://www.cnblogs.com/OvOq/p/14853215.html
Copyright © 2011-2022 走看看