题意:给你一组数\(a\),构造一个它的子序列\(b\),然后再求\(b_1-b2+b3-b4...\),问构造后的结果最大是多少. 题解:线性DP.我们用\(dp1[i]\)来表示在\(i\)位置,并且此时子序列的长度是奇数的情况,而\(dp2\)则是偶数情况,对于每个\(a_i\),\(dp[i]\)都可以选它或者不选,拿\(dp1[i]\)举例,如果选择\(a_i\),那么状态则可以从子序列中上一个位置转移过来,所以\(dp1[i]=dp2[i-1]+a[i]\),如果不选就是\(dp1[…
题意:给你两个长度为\(n\)的01串\(s\)和\(t\),可以选择\(s\)的前几位,取反然后反转,保证\(s\)总能通过不超过\(3n\)的操作得到\(t\),输出变换总数,和每次变换的位置. 题解:构造题一定要充分利用题目所给的条件,对于\(s\)中的某一位i,假如它和\(t\)中的对应位置不同,我们先对前i个字符取反反转,然后再对第一个字符取反反转(就选了一个,反不反都无所谓),在取前i个位置取反反转,这样,我们就将第i个位置变换了,消耗了3次操作.这样就一定能保证在\(3n\)之内完…
Codeforce 1420 C1. Pokémon Army (easy version) 解析(DP) 今天我們來看看CF1420C1 題目連結 題目 對於一個數列\(a\),選若干個數字,求alternating-series的最大值. 前言 C2真的想不到 @copyright petjelinux 版權所有 觀看更多正版原始文章請至petjelinux的blog 想法 \(dp[i][0]\)代表:考慮到第i個數字為止,最後一個數字是負的的最大值 \(dp[i][1]\)代表:考慮到第…
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an undirected tree of nn vertices. Some vert…
https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; int n; vector<int> color; vector<vector<int> > tree; ,blue=; ; pair<){ ); ); ;i<tree[v].size();i++){ int u=tree[v][i]; if(u!=p){//避免回…
This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems. After struggling and failing many times, Ujan decided to try to clean up his house again. He…
B1. Character Swap (Easy Version) This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems. After struggling and failing many times, Ujan decided to tr…
题意:有\(n\)本书,A和B都至少要从喜欢的书里面读\(k\)本书,如果一本书两人都喜欢的话,那么他们就可以一起读来节省时间,问最少多长时间两人都能够读完\(k\)本书. 题解:我们可以分\(3\)种情况来存,即: ​ 1.\(a=b=1\). 2.\(a=1,b=0\). 3.\(a=0,b=1\). 对于2和3来说,我们可以将他们排序,然后合并到一起,最后放到第1种情况中再排一次序,取前\(k\)个前缀和即可. 代码: int n,k; int t,x,y; int ans; vector…
题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\(n-len\). 感觉不好解释,直接上图,其实就是排序后它们一定是连续的,所以我们就求一个最长的连续的,然后s剩下的数移到头部尾部,贪心的想,这样一定是最优解. 代码: #include <iostream> #include <cstdio> #include <cstring…
[Codeforces Round #672 (Div. 2) A - C1 ] 题目链接# A. Cubes Sorting 思路: " If Wheatley needs more than \(\frac{n \cdot (n-1)}{2}-1\) exchange operations, he won't do this boring work." 例如:5,4,3,2,1 我们需要移动4+3+2+1=10次,也就是\(\frac{n \cdot (n-1)}{2}\)次,所以…
比赛链接:https://codeforces.com/contest/1420 A. Cubes Sorting 题意 给出一个大小为 $n$ 的数组 $a$,每次只可以交换相邻的两个元素,最多交换 $\frac{n \cdot (n-1)}{2}-1$ 次,判断能否将数组变为非递减序. 题解一 交换次数最多为 $\frac{n \cdot (n-1)}{2}$,此时数组为严格递减序,即 $a_1 > a_2 > \dots > a_{n - 1} > a_n$,从小到大每个元素…
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the follow…
题目链接: C1. Skyscrapers (easy version) 题目描述: 有一行数,使得整个序列满足 先递增在递减(或者只递增,或者只递减) ,每个位置上的数可以改变,但是最大不能超过原来的值. 最后找到满足这样的序列并且满足 这种方案 所有数加起来 和 是最大的. 考察点 : 贪心,对数据范围的掌握程度,计算每次加数时有可能会 爆 int 析题得侃: 比赛的时候看到这道题直接找了 最大值,然后以最大值为中心向两侧递减,交了一发, WA 后来想到可能会有重复的最大值,因为每个值并不是…
题目链接:https://codeforces.com/contest/1420/problem/D 前言 之前写过这场比赛的题解,不过感觉这一题还可以再单独拿出来好好捋一下思路. 题意 给出 $n$ 个闭区间,问 $k$ 个区间共区间共有多少种情况. 题解一 以区间为单位进行考虑,排序+优先队列. 将所有区间以左端点为第一关键字,右端点为第二关键字从小到大排序,优先队列中存储不小于当前区间左端点的之前区间的右端点,每个区间对答案的贡献即 $C_{(pque.size(),\ k - 1)}$…
D1:思路:L,R指针移动,每次选最小的即可. #include<bits/stdc++.h> using namespace std; #define int long long #define N 200009 int arr[N]; int ans[N]; signed main(){ int n; cin>>n; ;i<=n;i++){ cin>>arr[i]; } ,r=n; ; ; ){ if(now<arr[l]&&now<…
This is an easier version of the problem. In this version n≤1000n≤1000 The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlsk…
题目链接 题目大意 给你一个长为n(n<=1e5)的数组,让你求有多少对a[i]和a[j] (i!=j)满足a[i]&a[j]>a[i]^a[j] 题目思路 这些有关位运算的题目肯定是要把数字变成二进制的 在二进制中某一位的数只能是0或者1 0^0=0 0&0=0 0^1=1 0&1=0 1^0=1 1&0=0 1^1=0 1&1=0 这样你就会发现如果要&操做所获得的值更大,只有可能是最高位的1所处的位置相同 然后计算一下贡献即可 代码 #in…
题意:给你\(n\)个区间,从这\(n\)区间中选\(k\)个区间出来,要求这\(k\)个区间都要相交.问共有多少种情况. 题解:如果\(k\)个区间都要相交,最左边的区间和最右边的区间必须要相交,即\(min(r[1],...,r[k])>=max(l[1],...,l[k])\).我们先按左边界对所有区间进行排序,然后遍历左边界,遍历到某个区间时,说明这个区间的左边界目前是最大的,然后我们再判断当前左边界(\(l[i]\)就是最大的)和集合中右边界(\(rs.begin()\),一定是满足条…
题意:给你一组数,求有多少对\((i,j)\),使得\(a_{i}\)&\(a_{j}\ge a_{i}\ xor\ a_{j}\). 题解:对于任意两个数的二进制来说,他们的最高位要么相同要么不相同,如果相同,那么肯定是满足题目条件的,因为异或是不进位的加法,所以我们只要找到所有最高位相同的数的个数,用桶存下来,然后再对他们求个和就行了. 代码: int t; int n; ll x; map<ll,ll> mp; int main() { ios::sync_with_stdio(…
题意:有一长度为\(n\)的一组数,每次可以交换两个数的位置,问能否在\(\frac{n*(n-1)}{2}-1\)次操作内使得数组非递减. 题解:不难发现,只有当整个数组严格递减的时候,操作次数是\(\frac{n*(n-1)}{2}\),所以我们可以直接遍历判断有无\(a[i]<=a[i+1]\)即可. 代码: int t; int n; int a[N]; int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); ci…
任意门:http://codeforces.com/contest/1114/problem/D D. Flood Fill time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a line of nn colored squares in a row, numbered from 11 to nn f…
C. Famil Door and Brackets 题目连接: http://www.codeforces.com/contest/629/problem/C Description As Famil Door's birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of roun…
题目链接: http://codeforces.com/problemset/problem/208/C C. Police Station time limit per test:2 secondsmemory limit per test:256 megabytes 问题描述 The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to…
B. Let's Play Osu! 题目连接: http://www.codeforces.com/contest/235/problem/B Description You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us deno…
C. Chain Reaction 题目连接: http://www.codeforces.com/contest/608/problem/C Description There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys…
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度,我们想一下O(n^2)时的最长递增子序列,我们第二个循环要遍历前面所有的如果大于长度就加1,代表以这个数结尾的最长长度是多少, 因为中间差值不定,所以我们遍历整个循环,这个题设置中间差值只能是1,所以我们递推式就可以是   dp[i]=max(dp[i],dp[i-1]+1),用map来映射值即可…
C. Mahmoud and a Message 题目连接: http://codeforces.com/contest/766/problem/C Description Mahmoud wrote a message s of length n. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was su…
题目链接: http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test2 secondsmemory limit per test256 megabytes 问题描述 The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the…
题目链接: http://codeforces.com/problemset/problem/621/E E. Wet Shark and Blocks time limit per test2 secondsmemory limit per test256 megabytes 问题描述 There are b blocks of digits. Each one consisting of the same n digits, which are given to you in the inp…
题目链接: http://codeforces.com/problemset/problem/337/D D. Book of Evil time limit per test2 secondsmemory limit per test256 megabytes 问题描述 Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements num…