2020.10.16-10.17补题报告
Let's call a list of positive integers a0,a1,...,an−1a0,a1,...,an−1 a power sequence if there is a positive integer cc, so that for every 0≤i≤n−10≤i≤n−1 then ai=ciai=ci.
Given a list of nn positive integers a0,a1,...,an−1a0,a1,...,an−1, you are allowed to:
- Reorder the list (i.e. pick a permutation pp of {0,1,...,n−1}{0,1,...,n−1} and change aiai to apiapi), then
- Do the following operation any number of times: pick an index ii and change aiai to ai−1ai−1 or ai+1ai+1 (i.e. increment or decrement aiai by 11) with a cost of 11.
Find the minimum cost to transform a0,a1,...,an−1a0,a1,...,an−1 into a power sequence.
Input
The first line contains an integer nn (3≤n≤1053≤n≤105).
The second line contains nn integers a0,a1,...,an−1a0,a1,...,an−1 (1≤ai≤1091≤ai≤109).
Output
Print the minimum cost to transform a0,a1,...,an−1a0,a1,...,an−1 into a power sequence.
Examples
3 1 3 2
1
3 1000000000 1000000000 1000000000
1999982505
Note
In the first example, we first reorder {1,3,2}{1,3,2} into {1,2,3}{1,2,3}, then increment a2a2 to 44 with cost 11 to get a power sequence {1,2,4}{1,2,4}.
题意:对序列中的任意元素进行一步操作:对任意元素进行加一或减一,求让序列a0~an-1分别对应一个整数C的0-(n-1)次方,需要的最小操作数。
解题思路:先把序列从小到大排序,当n为1时,那个数一定对应的C的0次方;n=2时,令c=a[1];对于更大的数我们可以直接暴力。
#include<iostream> #include<algorithm> #include<cstring> #include<map> #include<cmath> #include<queue> #include<sstream> #include<vector> using namespace std; #define ll long long const ll inf=1e18; ll a[120000],n; int main(){ ll s=0,ss=0; cin>>n; for(int i=0;i<n;i++) {cin>>a[i]; ss+=a[i];} sort(a,a+n); if(n>=53) cout<<ss-n<<endl; else { if(n==1||n==2) cout<<a[0]-1<<endl; else { ll maxx=inf,flag=1,oo=0; for(ll i=2;i<=100000;i++) { ll q=0,tt=1; for(ll j=0;j<n;j++) { if(tt>1e14) { flag=0; break; } q+=abs(a[j]-tt); tt*=i; } if(flag==0)break; if(maxx>q) { oo=i; maxx=q; } } ll w=min(maxx,ss-n); cout<<w<<endl; } } return 0; }
D - Drinks Choosing
Old timers of Summer Informatics School can remember previous camps in which each student was given a drink of his choice on the vechorka (late-evening meal). Or may be the story was more complicated?
There are nn students living in a building, and for each of them the favorite drink aiai is known. So you know nn integers a1,a2,…,ana1,a2,…,an, where aiai (1≤ai≤k1≤ai≤k) is the type of the favorite drink of the ii-th student. The drink types are numbered from 11 to kk.
There are infinite number of drink sets. Each set consists of exactly two portions of the same drink. In other words, there are kk types of drink sets, the jj-th type contains two portions of the drink jj. The available number of sets of each of the kk types is infinite.
You know that students will receive the minimum possible number of sets to give all students exactly one drink. Obviously, the number of sets will be exactly ⌈n2⌉⌈n2⌉, where ⌈x⌉⌈x⌉ is xx rounded up.
After students receive the sets, they will distribute their portions by their choice: each student will get exactly one portion. Note, that if nn is odd then one portion will remain unused and the students' teacher will drink it.
What is the maximum number of students that can get their favorite drink if ⌈n2⌉⌈n2⌉ sets will be chosen optimally and students will distribute portions between themselves optimally?
Input
The first line of the input contains two integers nn and kk (1≤n,k≤10001≤n,k≤1000) — the number of students in the building and the number of different drinks.
The next nn lines contain student's favorite drinks. The ii-th line contains a single integer from 11 to kk — the type of the favorite drink of the ii-th student.
Output
Print exactly one integer — the maximum number of students that can get a favorite drink.
Examples
5 3 1 3 1 1 2
4
10 3 2 1 3 2 3 3 1 3 1 2
9
Note
In the first example, students could choose three sets with drinks 11, 11 and 22 (so they will have two sets with two drinks of the type 11 each and one set with two drinks of the type 22, so portions will be 1,1,1,1,2,21,1,1,1,2,2). This way all students except the second one will get their favorite drinks.
Another possible answer is sets with drinks 11, 22 and 33. In this case the portions will be 1,1,2,2,3,31,1,2,2,3,3. Then all the students except one will gain their favorite drinks. The only student that will not gain the favorite drink will be a student with ai=1ai=1 (i.e. the first, the third or the fourth).
题意:输入n和k,n是学生的数量,k是饮料种类,接下来的n行会输入每个学生想要的饮料的编号(1~k),分配饮料是按一对一对分,每一对都是类型相同的饮料
要求输出能得到自己想要饮料的最大学生数量。
解题思路:把喜欢同一种饮料的人归为一类,然后优先给喜欢同一种饮料且是偶数个人发饮料,这样两个人都能喝到自己喜欢的饮料。然后如果还有剩余整盒的(就是两瓶一起),每盒就只能有一个人喝到自己喜欢的了,另一个人肯定是喝不到了。
#include<iostream> #include<algorithm> #include<cmath> #include<map> using namespace std; int main(){ int n,m,i,a,c,d; cin>>n>>m; map<int,int> mp; mp.clear(); for(i =0;i<n;i++){ cin>>a; mp[a]++; } int ans=0; c=0; d=0; for(i=1;i<=m;i++){ c+=mp[i]%2; d+=mp[i]/2*2; } ans =d+(c+1)/2; cout<<ans<<endl; return 0; }