Codeforces 1244E. Minimizing Difference】的更多相关文章

(点击此处查看原题) 题意分析 给出n个数,a1,a2...an,现在可以进行最多k次操作,每次操纵可以使得任意一个数自增或者自减,问经过最多k次操作后,n个数中的最大值-最小值最小为多少? 解题思路 一开始,看到k最大可达1e14,就知道这个肯定不能暴力求解...但是我们可以通过优化暴力的方法来解题 注意到,我们只需要考虑最大值和最小值之差,因此我们要么使得最小值增加,要么使得最大值减少,但是每个数一个个地增加太慢,所以我们记录下每个数的数量和值,这样就可以一次性将最小值或者最小值进行增减了…
传送门 首先减的顺序是无关紧要的,那么有一个显然的贪心 每次减都减最大或者最小的,因为如果不这样操作,最大的差值不会变小 那么直接把序列排序一下然后模拟一下操作过程即可,别一次只减 $1$ 就好 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; typedef long lon…
Codeforces 903D Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Let's denote a function You are given an array a consisting of n integers. You have to calculate the sum o…
链接:http://codeforces.com/contest/1244/problem/E 题意: 给定包含$n$个数的数组,你可以执行最多k次操作,使得数组的一个数加1或者减1. 问合理的操作,使得数组中最大的数和最小的数差值最小. 思路: 二分答案,重点是检查的时候需要跑两遍. // #pragma GCC optimize(2) // #pragma GCC optimize(3) // #pragma GCC optimize(4) #include <algorithm> #in…
Codeforces 题目传送门 & 洛谷题目传送门 震惊!我竟然独立切掉了这道 *3100 的题! 虽然此题难度的确虚高,感觉真实评分也就 2800~2900 罢.但感觉还是挺有成就感的( 注意到题目中所询问的那坨东西基于每个数在区间中出现的次数,因此此题必不可少的一个步骤就是求出每个数的出现次数 \(cnt_x\),这个可以用带修莫队在 \(n^{5/3}\) 的时间内求出,这里就不再赘述了. 接下来考虑怎样计算答案,这也是本题的难点所在.我们将所有 \([l,r]\) 中出现次数非零的数的…
题目链接: https://vjudge.net/contest/336389#problem/B 题目大意: 给出一个长度为n的数列以及操作次数k.k的范围为1e14.每次操作都可以选择给任意一个数加1或者减1.问k次操作后,数列中最大值与最小值的差值最小是多少. 解题思路: 1.可以用二分 2.思维,对数组进行排序,同时计算前后两个方向.具体看代码: #include<stdio.h> #include<algorithm> typedef long long ll; ; us…
题意:给n个数,可以有k次的 + 1或 - 1,在k次操作之内,让n个数的最大值和最小值差最小. 思路:要让max和min的差值最小,也就等同于min--,max++,如果k==0结束操作,或者min==maxx也结束操作.所以先从小到大排序,在变化min和max的值时,谁的个数少先变化谁(因为只有把所有的max或者min都变化才算成功让差值最小),更新max或者min,统计min或max的个数. #include<algorithm> #include<stdio.h> #inc…
codeforces #592(Div.2) A Pens and Pencils Tomorrow is a difficult day for Polycarp: he has to attend a lectures and b practical classes at the university! Since Polycarp is a diligent student, he is going to attend all of them. While preparing for th…
link:http://codeforces.com/contest/347/problem/A 开始看起来很复杂的样子,但是刚写下样例,就发现因为中间的都消去了,其实起作用的就是最大值和最小值=_= #include <cstdio> #include <algorithm> using namespace std; ]; int main(void) { #ifndef ONLINE_JUDGE freopen("in.txt", "r"…
题目链接:http://codeforces.com/problemset/problem/347/A 题目意思:给出一个序列 a1, a2, ..., an , 通过重排序列,假设变成 x1, x2, ..., xn ,使得 (x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn)   的和最大.输出这个重排后的序列,不过这个序列在满足得到这个最大和的基础上字典序的排序是最小的.字典序排列即: Sequence x1, x2, ... , xp is lexico…
传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k,则答案一定是-1. 然后,考虑若n==a^k,枚举k,二分求a.若n==a^x*(a+1)^y,枚举x,y,二分求解a. 注意:两数相乘可能>1e18,特判. #include<bits/stdc++.h> using namespace std; typedef long long ll…
D. Almost Difference Let's denote a function You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n. Input The first line contains one integer n (1 ≤ n ≤ 200000) — t…
D. Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Let's denote a function You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) o…
D. Binary String Minimizing You are given a binary string of length n (i. e. a string consisting of n characters '0' and '1'). In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you c…
链接: https://codeforces.com/contest/1230/problem/B 题意: Ania has a large integer S. Its decimal representation has length n and doesn't contain any leading zeroes. Ania is allowed to change at most k digits of S. She wants to do it in such a way that S…
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n; cin >> n; vector<int> a(n); ; i < n; ++ i ) cin >>a[i]; sort(a.begin(),a.end()); swap(a[],a[n-]); ; i < n; ++i) co…
题意:给定 n 个数,让你找出一个排列满足每个数相邻作差之和最大,并且要求字典序最小. 析:这个表达式很简单,就是把重新组合一下,就成了x1-xn,那么很简单,x1是最大的,xn是最小的,中间排序就好. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <cstring> using namespace std;…
给你一个序列,让你求(x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn).值最大的一个序列,我们化简一下公式就会发现(x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn). = x1 - xn, 也就是说只有第一个和最后一个是确定的,其他的随便了!  也不是了, 还要让你按字典序最小的排列,也就是说其他的是按飞递减序排列的,简单的一次排序就OK了. //2013-09-21-09.07 #include <stdio.h> #i…
You are given a binary string of length nn (i. e. a string consisting of nn characters '0' and '1'). In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given o…
A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of size n. Nicholas want the minimum element (integer 1) an…
题目链接:http://codeforces.com/problemset/problem/337/A 题意:有n个学生,m块puzzles,选出n块puzzles,但是需要满足这n块puzzles里的最大pieces(A)和最小pieces(B)之差最少,即the least possible difference. 这是一道贪心兼排序的题目,解决方法不难.首先对m块puzzles以非递减的顺序排序(可以保证每个n长度的区间difference最小),接着求出所有长度为n的区间中最大和最小的值…
Predict Outcome of the Game Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 451C Description There are n games in a football tournament. Three teams are participating in it. Currently k …
MUH and House of Cards Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 471C Description Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo o…
链接:http://codeforces.com/contest/459/problem/B B. Pashmak and Flowers time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pashmak decided to give Parmida a pair of flowers from the garden. Ther…
Camping Groups 题目连接: http://codeforces.com/problemset/problem/173/E Description A club wants to take its members camping. In order to organize the event better the club directors decided to partition the members into several groups. Club member i has…
There is No Alternative 题目连接: http://codeforces.com/gym/100803/attachments Description ICPC (Isles of Coral Park City) consist of several beautiful islands. The citizens requested construction of bridges between islands to resolve inconveniences of u…
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server:…
B. Preparing Olympiad Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/problem/B Description You have n problems. You have estimated the difficulty of the i-th one as integer ci. Now you want to prepare a problemset for…
A. Puzzles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/337/A Description The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to p…
C. Little Elephant and Shifts Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/220/C Description The Little Elephant has two permutations a and b of length n, consisting of numbers from 1 to n, inclusive. Let's de…