Codeforces Round #521 (Div. 3) C. Good Array
1 second
256 megabytes
standard input
standard output
Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array a=[1,3,3,7]a=[1,3,3,7] is good because there is the element a4=7a4=7 which equals to the sum 1+3+31+3+3 .
You are given an array aa consisting of nn integers. Your task is to print all indices jj of this array such that after removing the jj -th element from the array it will be good (let's call such indices nice).
For example, if a=[8,3,5,2]a=[8,3,5,2] , the nice indices are 11 and 44 :
- if you remove a1a1 , the array will look like [3,5,2][3,5,2] and it is good;
- if you remove a4a4 , the array will look like [8,3,5][8,3,5] and it is good.
You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.
The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105 ) — the number of elements in the array aa .
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106 ) — elements of the array aa .
In the first line print one integer kk — the number of indices jj of the array aa such that after removing the jj -th element from the array it will be good (i.e. print the number of the nice indices).
In the second line print kk distinct integers j1,j2,…,jkj1,j2,…,jk in any order — nice indices of the array aa .
If there are no such indices in the array aa , just print 00 in the first line and leave the second line empty or do not print it at all.
- 5
- 2 5 1 2 2
- 3
- 4 1 5
- 4
- 8 3 5 2
- 2
- 1 4
- 5
- 2 1 2 4 3
- 0
In the first example you can remove any element with the value 22 so the array will look like [5,1,2,2][5,1,2,2] . The sum of this array is 1010 and there is an element equals to the sum of remaining elements (5=1+2+25=1+2+2 ).
In the second example you can remove 88 so the array will look like [3,5,2][3,5,2] . The sum of this array is 1010 and there is an element equals to the sum of remaining elements (5=3+25=3+2 ). You can also remove 22 so the array will look like [8,3,5][8,3,5] . The sum of this array is 1616 and there is an element equals to the sum of remaining elements (8=3+58=3+5 ).
In the third example you cannot make the given array good by removing exactly one element.
首先思路是预处理一个数组maxx,维护除了i位置a[i]以外的最大值。然后求sum=sigma(a[i]),遍历一遍,if(maxx[i]==(sum-a[i]-maxx[i]))
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn=;
- int a[maxn],n,maxl[maxn],maxr[maxn],maxx[maxn],ans[maxn];
- ll sum;
- int main() {
- scanf("%d",&n);
- for(int i=;i<=n;i++) {
- scanf("%d",&a[i]);
- sum+=a[i];
- }
- for(int i=;i<=n;i++) {
- maxl[i]=max(a[i-],maxl[i-]);
- }
- for(int i=n-;i>=;i--) {
- maxr[i]=max(a[i+],maxr[i+]);
- }
- for(int i=;i<=n;i++) maxx[i]=max(maxl[i],maxr[i]);
- int cnt=;
- for(int i=;i<=n;i++) {
- if(maxx[i]==sum-a[i]-maxx[i]) {
- ans[cnt++]=i;
- }
- }
- if(!cnt) printf("0\n");
- else {
- printf("%d\n",cnt);
- for(int i=;i<cnt;i++) printf("%d ",ans[i]);
- }
- }
本以为这样挺好的,后来发现可以更简单,看了题解,若有所思。 因为除去a[i]以后,如果存在这样的好位置,那么剩余的数之和一定是偶数,(因为就是2倍的最大值)。然后判断最大值存不存在即可。
代码为正解:
- #include <bits/stdc++.h>
- using namespace std;
- const int MAX = 1e6;
- int main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- #endif
- int n;
- cin >> n;
- vector<int> a(n);
- vector<int> cnt(MAX + );
- for (int i = ; i < n; ++i) {
- cin >> a[i];
- ++cnt[a[i]];
- }
- long long sum = accumulate(a.begin(), a.end(), 0ll);
- vector<int> ans;
- for (int i = ; i < n; ++i) {
- sum -= a[i];
- --cnt[a[i]];
- if (sum % == && sum / <= MAX && cnt[sum / ] > ) {
- ans.push_back(i);
- }
- sum += a[i];
- ++cnt[a[i]];
- }
- cout << ans.size() << endl;
- for (auto it : ans) cout << it + << " ";
- cout << endl;
- return ;
- }
Codeforces Round #521 (Div. 3) C. Good Array的更多相关文章
- Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)
Codeforces Round #521 (Div. 3) E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...
- Codeforces Round #258 (Div. 2) . Sort the Array 贪心
B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...
- Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】
任意门:http://codeforces.com/contest/1077/problem/D D. Cutting Out time limit per test 3 seconds memory ...
- CodeForces Round #521 (Div.3) D. Cutting Out
http://codeforces.com/contest/1077/problem/D You are given an array ss consisting of nn integers. Yo ...
- CodeForces Round #521 (Div.3) B. Disturbed People
http://codeforces.com/contest/1077/problem/B There is a house with nn flats situated on the main str ...
- Codeforces Round #555 (Div. 3) E. Minimum Array 【数据结构 + 贪心】
一 题面 E. Minimum Array 二 分析 注意前提条件:$0 \le a_{i} \lt n$ 并且 $0 \le b_{i} \lt n$.那么,我们可以在$a_{i}$中任取一个数 ...
- CodeForces Round #521 (Div.3) E. Thematic Contests
http://codeforces.com/contest/1077/problem/E output standard output Polycarp has prepared nn competi ...
- Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)
F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...
- CodeForces Round #521 (Div.3) A. Frog Jumping
http://codeforces.com/contest/1077/problem/A A frog is currently at the point 00 on a coordinate axi ...
随机推荐
- C Primer Plus学习笔记(四)- 运算符、表达式和语句
基本运算符 赋值运算符:= 在C语言中,=不是“相等”,而是赋值运算符,把左边的值赋给右边的变量 a = 2018; //把值2018赋给变量a 赋值表达式语句的目的是把值储存到内存位置上,用于储存值 ...
- 2015.11.3 RichBox改变若干文本颜色
for(int i=1;i<rtb.Lines.Length;i++) { if(rtb.Lines[i] == rtb.Lines[i - 1]) { int bg = rtb.GetFirs ...
- orancle数据库 插入数量 值大于 1000 解决方案
存储过程:当基站ID大于1000的时候,把ID通过存储过程插入表,然后处理 不推荐这么弄,没办法,项目逼到这了,以后尽量避免这样的需求发生! CREATE OR REPLACE PROCEDURE i ...
- linux中创建图片服务器减轻传统服务器的压力
1.1. 传统项目中的图片管理 传统项目中,可以在web项目中添加一个文件夹,来存放上传的图片.例如在工程的根目录WebRoot下创建一个images文件夹.把图片存放在此文件夹中就可以直接使用在 ...
- Win10_禁用自动更新(官方版)
1> win键>输入服务>打开>找到windowsUpdate-->启动类型为-禁用 -->> 恢复失三个选项改为-->>无操作 2>win ...
- String/StringBuilder 类 统计字符串中字符出现的次数
1.1. 训练描述:[方法.String类] 一.需求说明:定义如下字符串: String str = “javajfiewjavajfiowfjavagkljjava”; 二.请分别定义方法统计出: ...
- 前端学习笔记2017.6.12 HTML的结构以及xhtml、html、xml的区别
HTML的结构 一个HTML文档可分为几个部分,如下图所示: DOCTYPE部分.head部分和body部分 DOCTYPE部分,这个很重要,可以理解为不同的DOCTYPE意味着不同的html标准,因 ...
- 547D Mike and Fish
传送门 分析 见正睿10.3笔记 代码 #include<iostream> #include<cstdio> #include<cstring> #include ...
- 安装Maven及Eclipse中配置Maven
下载maven版本: 1.进入官网:http://maven.apache.org/download.cgi ,下载编译后的maven版本:如图下: 2.创建一个目录,把下载的maven压缩包,进 ...
- JaVA web服务器配置
1:第一是下载好Eclipse开发工具,这里不做叙述,自行下载安装. 2:使用Eclipse开发WEB项目,启动Eclipse,选择File--->new --->other---> ...