CF 1141C Polycarp Restores Permutation】的更多相关文章

Description An array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each number from 11 to nn exactly once. For example, the following arrays are permutations: [3,1,2][3,1,2] , [1][1] , [1,2,3,4,5][1,2,3,4,5] and [4,3,1,2][4,…
链接 [https://codeforces.com/contest/1141/problem/C] 题意 qi=pi+1−pi.给你qi让你恢复pi 每个pi都不一样 分析 就是数学吧 a1 +(a2-a1) +(a3-a1) +(a4-a1) +(a5-a1) +(a6-a1) +-+(an-a1)=a1+a2+....+an-(n-1)a1; =n(n+1)/2-(n-1)*a1=a1+(a2-a1) +(a3-a1) +(a4-a1) +(a5-a1) +(a6-a1) +-+(an-a…
http://codeforces.com/contest/1141/problem/C一开始没想法暴力的,next_permutation(),TLE 后来看了这篇https://blog.csdn.net/kuronekonano/article/details/88679703后来又看了这篇http://www.cnblogs.com/YDDDD/p/10570961.html,懂了,第一个数其实也没必要求出来,就像第一篇画的那个图里那样,其实你只要把那个折线图整体上下移让它落在正确的范围…
\(problem\) 这题的大致意思就是已知数值差值 求1-n的排列 如果能构成排列 则输出这个排列.如果不能则输出-1 排列的值都是 大于1 而小于n的 而且没有相同的数字. 这题最关键的是 怎么输出这个序列 有的是存在负数的. 那么 考虑一下排列都是从1到n的对不对. 取序列的最小值 然后用\(1 - Min\)即是整个序列应该加上的数值. 首先考虑判重. 用数组 或者用\(Map\) \(or\) \(Set\) . 都是不错的方法. #include <bits/stdc++.h>…
题意:有一长度为\(n\)的序列\(p\),现在给你\(q_i=p_{i+1}-q_i \ (1\le i\le n)\),问你是否能还原出原序列,如果能救输出原序列,否则输出\(-1\). 题解:由:\(q_i=p_{i+1}-p_i\),我们对其求前缀和可得:\(s_i=p_{i+1}-p_1\),然后再求出:\(\sum^{n-1}_{i=1}s_i=\sum^{n}_{i=2}p_i-(n-1)*p_1\),\(\sum^{n}_{i=2}p_i=\sum^{n}_{i=1}i-p_1=…
Content 给定一个长度为 \(n-1\) 的序列 \(q\),问你是否能找到一个 \(1\sim n\) 的排列 \(p\),使得 \(\forall i\in[1,n)\),\(q_i=p_{i+1}-p_i\). 数据范围:\(2\leqslant n\leqslant 2\times 10^5\),\(-n<q_i<n\). Solution 首先我们不难知道,设 \(S_i=\sum\limits_{j=1}^i q_j\),那我们不难知道,\(S_i\) 越小,第 \(i+1\…
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a1,a2,-,an, respectively. His plan is to practice for exactly k days. Each day he has to solve at least one problem from his list. Polycarp solves the pro…
http://codeforces.com/contest/361/problem/B #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n,k; int main() { while(scanf("%d%d",&n,&k)!=EOF) { if(n==k) { printf("-1\n"); con…
传送门 题目大意 给你一个数列,再给你一个矩阵,矩阵的(i,j)如果为1就表示可以将i,j位置上的数交换,问任意交换之后使原数列字典序最小并输出. 解题思路 因为如果i与j能交换,j与k能交换,那么i与k相当于能直接交换,所以我们先使用传递闭包求出所有可以交换的情况.之后从第一个位置开始贪心,看它能跟后面哪个小于它的数交换. 代码 #include<iostream> #include<cstdio> #include<cstring> using namespace…
Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restores Permutation 题意: 有一个n的排列,但现在只给出相邻两位的差,问原排列是多少,如果不存在就输出-1. 题解: 通过相邻两位的差我们可以知道第一个元素和其它位置元素的大小关系,然后根据这个来搞一波就行了. 代码如下: #include <bits/stdc++.h> using n…