Codeforces 808D Array Division【思维】

本文探讨了一种算法问题,即如何通过移动数组中的一个元素来实现数组的有效分割,确保分割后的两个子数组的元素之和相等。文章提供了解决方案的详细步骤,并讨论了不同情况下的处理方法。

D. Array Division
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).

Inserting an element in the same position he was erased from is also considered moving.

Can Vasya divide the array after choosing the right element to move and its new position?

Input

The first line contains single integer n (1 ≤ n ≤ 100000) — the size of the array.

The second line contains n integers a1, a2... an (1 ≤ ai ≤ 109) — the elements of the array.

Output

Print YES if Vasya can divide the array after moving one element. Otherwise print NO.

Examples
Input
3
1 3 2
Output
YES
Input
5
1 2 3 4 5
Output
NO
Input
5
2 2 3 4 5
Output
YES
Note

In the first example Vasya can move the second element to the end of the array.

In the second example no move can make the division possible.

In the third example Vasya can move the fourth element by one position to the left.


题目大意:

给你一个长度为N的序列,让你将其分成连续的两部分(不能为空),使得两部分的和都相等。

我们在分序列之前,可以选择不移动或者移动一个数到任意一个位子。


思路:


很套路,但是很简单。

我们首先维护出一个前缀和。

然后O(n)枚举分界线。

那么前一部分的和为Sumpre,后一部分的和为Sumback.

一共有三种情况:
①Sumpre==Sumback.那么结果就是YES.

②Sumpre>Sumback.那么我们考虑将前一部分的序列中,调出一个值为(Sumpre-Sumback)/2的值即可。这里map维护一下

③Sumpre<Sumback.那么我们考虑将后一部分的序列中,调出一个值为(Sumback-Sumpre)/2的值即可。这里map维护一下


注意序列不能为空。

注意奇偶。

以及某个值A【i】==sum【n】/2的情况也是YES.


Ac代码:

#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
#define ll __int64
ll a[1000050];
ll sum[1000050];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int flag=0;
        map<ll,int >pre;
        map<ll,int >back;
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
        for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];
        for(int i=1;i<=n;i++)back[a[i]]++;
        for(int i=1;i<=n;i++)if(sum[n]%2==0&&a[i]==sum[n]/2)flag=1;
        for(int i=1;i<n;i++)
        {
            back[a[i]]--;
            pre[a[i]]++;
            ll Sumpre=sum[i];
            ll Sumback=sum[n]-sum[i];
            if(Sumpre==Sumback)flag=1;
            else
            {
                if(Sumpre>Sumback)
                {
                    if(i==1)continue;
                    if((Sumpre-Sumback)%2==1)continue;
                    ll x=(Sumpre-Sumback)/2;
                    if(pre[x]>0)flag=1;
                }
                else
                {
                    if(i==n-1)continue;
                    if((Sumback-Sumpre)%2==1)continue;
                    ll x=(Sumback-Sumpre)/2;
                    if(back[x]>0)flag=1;
                }
            }
        }
        if(flag==1)printf("YES\n");
        else printf("NO\n");
    }
}







### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值