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 seco…
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 consecuti…
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 consecuti…
题目大意 给定你一个长为\(n\)的序列,问能否在最多一次取出某一元素然后插入到某一点后可以将整个序列分成两段使得其两段的元素之和相同. \(n \leq 10^5\) 题解 发现插入操作实际上是让某一个元素与端点周围的元素交换. 维护一个支持插入和查找元素是否存在的ds即可. #include <set> #include <cstdio> #include <cstring> #include <algorithm> using namespace st…
题目链接:http://codeforces.com/contest/808/problem/D 题意:有一串长度为n的数组,要求选择一个数字交换它的位置使得这串数能够分成两串连续的和一样的数组. 这个数还可以和自己交换位置. 题解:很显然求一下前缀二分每个数看一下能否插入,再求一下后缀二分每个数看一下能否插入. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm…
题目链接:http://codeforces.com/contest/808/problem/D 题意: 这一题给你一个数组,你可以调换某一个数的位置,使得这个数组可以分成2半,前半段的和等于后半段(严格的前半段和后半段).问你能不能构成. 题解: 一开始读题的时候,被吓坏了,没有看到是移动一个,因为题目在output那里才有写是移动一个. 那么如果这个和是奇数的话,就无法分成2个相等的部分.则是NO. 如果这个数列里有一个数是sum/2的话也是YES. 如果是移动一个数,那么这个数一定在某一个…
传送门 题意 将n个数划分为两块,最多改变一个数的位置, 问能否使两块和相等 分析 因为我们最多只能移动一个数x,那么要么将该数往前移动,要么往后移动,一开始处理不需要移动的情况 那么遍历sum[i] 如果往前移动,sum[k]+(sum[i]-sum[i-1])=sum[n]/2,k∈[1,i-1] 如果往后移动,sum[k]-(sum[i]-sum[i-1])=sum[n]/2,k∈[i+1,n] 一开始我没有考虑往后移动 时间复杂度\(O(nlog(n))\) trick 代码 #incl…
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 seco…
B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错了... 用1 2 来构造 可以先枚举一些数来找找规律. 1 1 2 2 3 1 1 1    2 1 1 4 .... 可以发现每一个数都是 n/2+1 的可能, 所以反过来推过去就是 (s-1)*2  或者(s-1)*2+1 这个(s-1)*2+1的答案才是正确答案 因为 这个s可以==1 #i…
大意: 无向图, 无重边自环, 每个点度数>=3, 要求完成下面任意一个任务 找一条结点数不少于n/k的简单路径 找k个简单环, 每个环结点数小于n/k, 且不为3的倍数, 且每个环有一个特殊点$x$, $x$只属于这一个环 任选一棵生成树, 若高度>=n/k, 直接完成任务1, 否则对于叶子数一定不少于k, 而叶子反向边数>=2, 一定可以构造出一个环 #include <iostream> #include <algorithm> #include <c…
题面: You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 ≤ xi ≤ n, 0 < k < n) the sums of elements on that positions in a and b are different,…
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 100005 ],size; inline void in(int &am…
                                                                                                  G. New Roads                                                                                                time limit per test 2 seconds               …
http://codeforces.com/contest/808/problem/D 一开始是没什么想法的,然后回顾下自己想题的思路,慢慢就想出来了.首先要找到是否有这样的一个位置使得: 前缀和 == 后缀和,可以二分来求. 然后可以这样想,如果对于每一个数字,我都去移动一下,每个位置都试一下,复杂度多少?显然不能承受. 然后优化下这个思路,有了一点思路,优化到极致,看看能不能过,不能过就换思路吧.一般来说,每一个位置都试一下,是很没必要的.一般都是有一个位置是最优的. 这个位置就是放在最前或…
http://codeforces.com/contest/808/problem/D #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> #include<set> #include<map> #include<utility> usin…
题意:给你一颗树,树的边权都是偶数,并且边权各不相同.你可以选择树的两个叶子结点,并且把两个叶子结点之间的路径加上一个值(可以为负数),问是否可以通过这种操作构造出这颗树?如果可以,输出构造方案.初始树的边权都是0. 思路:A1很简单,只要判断是否有度数为2的点就可以了.对于A2, 由于边权各不相同,所以A1的结论同样适用.现在我们来构造一组答案.官方题解的构造方式是这样的:我们假设要让一个节点u到叶子结点v的路径都加上一个值x,并且知道叶子结点l1, l2都可以到达u,我们执行以下操作:v到l…
题解: 这里的m一定是等于n的,n为数最大为n个9,这n个9一定满足条件,根据题目意思,前k个一定是和原序列前k个相等,因此如果说我们构造出来的大于等于原序列,直接输出就可以了,否则,由于后m-k个一定是重复前k个,我们只能在前k个改动,所以只需要让前k个加1就行了,然后在根据题意构造一遍. #include<bits/stdc++.h> using namespace std; ; char s[N],s1[N]; void solve() { int n,k; cin>>n&g…
大意: 求将[1,n]划分成两个集合, 且两集合的和的差尽量小. 和/2为偶数最小差一定为0, 和/2为奇数一定为1. 显然可以通过某个前缀和删去一个数得到. #include <iostream> #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #inclu…
题目链接 非常好的一道题目, 分析,如果用暴力的话,时间复杂度是O(q*n)稳稳的超时 如果用二维DP的话,需要O (n*n)的空间复杂度,会爆空间. 那么分析一下,如果k>sqrt(n)的话,不需要sqrt(n) 次就可以达到大于n 而如果k<sqrt(n)的情况下,不妨用DP来处理,时间复杂度和空间复杂度皆为O ( n*sqrt( n ) ) 那么都可以分类处理了. 详细见code #include <iostream> #include <cstdio> #inc…
首先我们能注意到两个数x, y (0 < x , y < m) 乘以倍数互相可达当且仅当gcd(x, m) == gcd(y, m) 然后我们可以发现我们让gcd(x, m)从1开始出发走向它的倍数一个一个往里加元素就好啦, 往那边走 这个可以用dp求出来, dp[ i ] 表示 gcd(x, m)从 i 开始最大元素一共有多少个, dp[ i ] = max( dp[ j ] ) + cnt[ i ]   且 i | j 然后用扩展欧几里德求出走到下一步需要乘多少. #include<…
题意:给出一个数组,2种操作:.1:x*y然后x消失,2:除掉x(2操作最多只能进行一次).问最大的结果的一种操作方式.逻辑题,看能不能想全面. 1先数好0,正,负的数量,zero,pos,neg.如果0数量不为0,在所有0的内部用操作1减少到只剩1个0,zero置1:(删去0不影响结果,如果结果是0,那么剩1个0也能做到,如果结果不是0,那么删0是必须的) 2负数有奇数个时(这种情况下一定有非负解)(1)如果zero=0,用操作2删掉最大的负数(不删结果负,删了必为正)(2)zero=1,用0…
大意: 求从[1,n]范围选择尽量多的数对, 使得每对数的gcd>1 考虑所有除2以外且不超过n/2的素数p, 若p倍数可以选择的有偶数个, 直接全部划分即可 有奇数个的话, 余下一个2*p不划分, 其余全部划分 最后再将2的倍数全部划分一下即可 #include <iostream> #include <math.h> #include <string.h> #include <algorithm> #include <cstdio> #…
链接 大意: 给定n种珠子, 第i种有$a_i$个, 求将珠子穿成项链, 使得能使切开后的项链回文的切口尽量多 若有一种以上珠子为奇数, 显然不能为回文, 否则最大值一定是$gcd(a_1,a_2,...,a_n)$ 若有一个奇数, 直接分成$gcd(a_1,a_2,...,a_n)$块, 每块内奇数放中间, 其余对称分 无奇数的话, $gcd(a_1,a_2,...,a_n)$一定是2的倍数, 可以将2块和为1块, 两块间对称分即可 #include <iostream> #include…
题意:给出一个序列,询问是否能移动一个数(或不操作)使得序列能分为左右两个和相等的子序列. 思路:对每个数处理最左边和最右边出现的位置.设置断点分左右区间,左右区间和差值的一半就是要找的数,进行判断. tip:很简单的想法,但debug很久很久,细节部分考虑欠妥.忘记位运算的优先级很低,sub&1==1错误,l[sub]<=i时漏考虑sub不出现时l[sub]都为0,发生错误. #include<bits/stdc++.h> using namespace std; typede…
大意: 给定序列, 给定常数a,b, 两种操作, (1)任选一个长为$t$的子区间删除(不能全部删除), 花费t*a. (2)任选$t$个元素+1/-1, 花费t*b. 求使整个序列gcd>1的最少花费. 题目有个限制是不能全部删除, 所以最后一定剩余a[1]或a[n], 暴力枚举a[1]与a[n]的所有素因子即可. 这场div. 2题目感觉都挺简单的, 但实现起来各种出错...........各种细节还是没考虑好...... #include <iostream> #include &…
大意: 定义$n$元素序列$a$的美丽度为 $\min\limits_{1\le i<j\le n}|a_i-a_j|$. 给定序列$a$, 求$a$的所有长为$k$的子序列的美丽度之和. 记 长为$k$的相邻元素距离都$\ge x$的子序列个数 为$f(x)$. 那么答案就为$\sum\limits_{x=1}^{\frac{1e5}{k-1}} f(x)$. $f(x)$很容易可以$O(nk)$的$dp$求出, 总复杂度就为$O(1e5n)$. #include <iostream>…
大意: $n$个石子, 第$i$个石子初始位置$s_i$, 每次操作选两个石子$i,j$, 要求$s_i<s_j$, 任取$d$, 满足$0\le 2d\le s_j-s_i$, 将$s_i,s_j$改为$s_i+d,s_j-d$. 给定数组$t$, 求是否能将所有石子位置摆成数组$t$. 没要求最小化操作数, 所以直接贪心选即可, 操作数一定是不超过$n$的. 这场当时没时间打, 感觉好亏. #include <iostream> #include <sstream> #i…
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <b…
核心观察是形如01,001,0001,...的串循环时, $n$每增长1, $k$就增长1. #include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <stri…
大意: 给定字符串$s$,$t$, 每次操作可以将$S=AB$变为$S=B^RA$, 要求$3n$次操作内将$s$变为$t$. #include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue>…