zoukankan      html  css  js  c++  java
  • Splitting Pile

    Splitting Pile


    Time limit : 2sec / Memory limit : 256MB

    Score : 300 points

    Problem Statement

    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

    Input is given from Standard Input in the following format:

    N
    a1 a2  aN
    

    Output

    Print the answer.


    Sample Input 1

    6
    1 2 3 4 5 6
    

    Sample Output 1

    1
    

    If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10y=11, and thus |x−y|=1. This is the minimum possible value.


    Sample Input 2

    2
    10 -10
    

    Sample Output 2

    20
    

    Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, x=10y=−10, and thus |x−y|=20.

    题意:一共有n张牌,A拿走前面的至少一张,最多只剩一张,B拿走剩下的,问他们各自的牌的和的绝对值差最小是多少?

    直接暴力得了,把所有能拿的情况都列出来,然后排个序ok

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <queue>
     5 #include <algorithm>
     6 #include <vector>
     7 using namespace std;
     8 #define LL long long
     9 #define MX 200010
    10  
    11 LL a[MX];
    12 LL ans[MX];
    13  
    14 int main()
    15 {
    16     int n;
    17     scanf("%d",&n);
    18     LL sum=0;
    19     for(int i=0;i<n;i++)
    20     {
    21         cin>>a[i];
    22         sum+=a[i];
    23     }
    24     LL x = a[0];
    25     LL y = sum-a[0];
    26     ans[0]=abs(x-y);
    27     for (int i=1;i<n-1;i++)
    28     {
    29         x+=a[i];
    30         y-=a[i];
    31         ans[i]=abs(x-y);
    32     }
    33     sort(ans,ans+n-1);
    34     cout<<ans[0]<<endl;
    35     return 0;
    36 }
    View Code
  • 相关阅读:
    TCP Socket服务器编程[转文]
    http协议学习和总结系列[转 ]
    linux C函数大全
    HTTP 协议详解
    Pthread 多线程总结
    linux 中解析命令行参数 (getopt_long用法)
    微软企业库4.1学习笔记(十八)缓存模块6 缓存的设计目的
    微软企业库4.1学习笔记(十七)缓存模块5 缓存的典型用法
    进程和线程的区别
    C#二叉树遍历算法实现浅析
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/7186103.html
Copyright © 2011-2022 走看看