1. 程式人生 > 實用技巧 >2020.10.16-10.17補題報告

2020.10.16-10.17補題報告

B - Power Sequence

Let's call a list of positive integersa0,a1,...,an1a0,a1,...,an−1apower sequenceif there is a positive integercc, so that for every0in10≤i≤n−1thenai=ciai=ci.

Given a list ofnnpositive integersa0,a1,...,an1a0,a1,...,an−1, you are allowed to:

  • Reorder the list (i.e. pick a permutation
    ppof{0,1,...,n1}{0,1,...,n−1}and changeaiaitoapiapi), then
  • Do the following operation any number of times: pick an indexiiand changeaiaitoai1ai−1orai+1ai+1(i.e. increment or decrementaiaiby11) with a cost of11.

Find the minimum cost to transforma0,a1,...,an1a0,a1,...,an−1into a power sequence.

Input

The first line contains an integernn(3n1053≤n≤105).

The second line containsnnintegersa0,a1,...,an1a0,a1,...,an−1(1ai1091≤ai≤109).

Output

Print the minimum cost to transforma0,a1,...,an1a0,a1,...,an−1into a power sequence.

Examples

Input
3
1 3 2
Output
1
Input
3
1000000000 1000000000 1000000000
Output
1999982505

Note

In the first example, we first reorder{1,3,2}{1,3,2}into{1,2,3}{1,2,3}, then incrementa2a2to44with cost11to 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;
}
View Code

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 arennstudents living in a building, and for each of them the favorite drinkaiaiis known. So you knownnintegersa1,a2,,ana1,a2,…,an, whereaiai(1aik1≤ai≤k) is the type of the favorite drink of theii-th student. The drink types are numbered from11tokk.

There are infinite number of drink sets. Each set consists ofexactly twoportions of the same drink. In other words, there arekktypes of drink sets, thejj-th type contains two portions of the drinkjj. The available number of sets of each of thekktypes 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 exactlyn2⌈n2⌉, wherex⌈x⌉isxxrounded up.

After students receive the sets, they will distribute their portions by their choice: each student will get exactly one portion. Note, that ifnnis 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 ifn2⌈n2⌉sets will be chosen optimally and students will distribute portions between themselves optimally?

Input

The first line of the input contains two integersnnandkk(1n,k10001≤n,k≤1000) — the number of students in the building and the number of different drinks.

The nextnnlines contain student's favorite drinks. Theii-th line contains a single integer from11tokk— the type of the favorite drink of theii-th student.

Output

Print exactly one integer — the maximum number of students that can get a favorite drink.

Examples
Input
5 3
1
3
1
1
2
Output
4
Input
10 3
2
1
3
2
3
3
1
3
1
2
Output
9
Note

In the first example, students could choose three sets with drinks11,11and22(so they will have two sets with two drinks of the type11each and one set with two drinks of the type22, so portions will be1,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 drinks11,22and33. In this case the portions will be1,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 withai=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;
}
View Code