1. 程式人生 > 實用技巧 >2020.10.16--vj個人賽補題

2020.10.16--vj個人賽補題

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 drink

aiaiis 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 are

kktypes 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個學生住在一棟樓裡,每個人最喜歡的飲料ai是已知的。所以你知道n個整數a1 a2…an,其中ai(1≤ai≤k)是第i個學生最喜歡的飲料型別。飲料型別從1到k進行編號。

有無數種飲料。每一組包含兩份相同的飲料,也就是說,有k種類型的飲料集,即第j種包含了兩份飲料j,每一種型別的可用集的數量是無限的,

選出n/2種,x向上取最大,學生們收到這些套裝後,他們將根據自己的選擇分配他們的份額:每個學生將得到恰好一份。請注意,如果n是奇數,那麼有一部分將不使用,學生的老師將喝它。

問學生們將得到他們最喜歡的飲料的最大人數是多少?

輸入

輸入的第一行包含兩個整數n和k(1≤n,k≤1000)-建築物內的學生人數和不同飲料的數量。

接下來的n行包含學生最喜歡的飲料。第i行包含一個從1到k的整數——第i個學生最喜歡的飲料的型別。

輸出

準確地列印一個整數-可以得到最喜歡的飲料的學生的最大人數。

題解: 將每種型別的個數用陣列 b[] 記錄下來,設一變數ct=0,用於記載最大人數,因為只能選n/2個型別,每個型別包含兩種相同的飲料,所以當陣列元素大於等於2時,

b[i]-=2,ct+=2,(n/2)--;直至b[i]<2或m<=0

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
int main()
{
    int n,k,i,j;
    cin>>n>>k;
    int m=(n+1)/2;
    int ct=0;
    int b[maxn];
    memset(b,0,sizeof(b));
    while(n--)
    {
        int a;
        cin>>a;
        ++b[a];
    }
    sort(b,b+k+1,greater<int>());
    for(i=0;i<k;i++)
    {
            while(b[i]>=2&&m>0)
            {
                b[i]-=2;
                --m;
                ct+=2;
            }
    }
    cout<<ct+m<<endl;
}

E - Sport Mafia

Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia.

For the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performsnnactions. The first action performed is to put a single candy into the box. For each of the remaining moves she can choose from two options:

  • the first option, in case the box contains at least one candy, is to takeexactly one candy out and eat it. This way the number of candies in the box decreased by11;
  • the second option is to put candies in the box. In this case, Alya will put11more candy, than she put in the previous time.

Thus, if the box is empty, then it can only use the second option.

For example, one possible sequence of Alya's actions look as follows:

  • put one candy into the box;
  • put two candies into the box;
  • eat one candy from the box;
  • eat one candy from the box;
  • put three candies into the box;
  • eat one candy from the box;
  • put four candies into the box;
  • eat one candy from the box;
  • put five candies into the box;

This way she will perform99actions, the number of candies at the end will be1111, while Alya will eat44candies in total.

You know the total number of actionsnnand the number of candies at the endkk. You need to find the total number of sweets Alya ate. That is the number of moves of the first option. It's guaranteed, that for the givennnandkkthe answer always exists.

Please note, that during an action of the first option, Alya takes out and eats exactly one candy.

Input

The first line contains two integersnnandkk(1n1091≤n≤109;0k1090≤k≤109)— the total number of moves and the number of candies in the box at the end.

It's guaranteed, that for the givennnandkkthe answer exists.

Output

Print a single integer— the number of candies, which Alya ate. Please note, that in this problem there aren't multiple possible answers— the answer is unique for any input data.

Examples

Input
1 1
Output
0
Input
9 11
Output
4
Input
5 0
Output
3
Input
3 2
Output
1

Note

In the first example, Alya has made one move only. According to the statement, the first move is always putting one candy in the box. Hence Alya ate00candies.

In the second example the possible sequence of Alya's actions looks as follows:

  • put11candy,
  • put22candies,
  • eat a candy,
  • eat a candy,
  • put33candies,
  • eat a candy,
  • put44candies,
  • eat a candy,
  • put55candies.

This way, she will make exactlyn=9n=9actions and in the end the box will contain1+211+31+41+5=111+2−1−1+3−1+4−1+5=11candies. The answer is44, since she ate44candies in total.

大體題意:為了比賽,Alya在盒子裡放了糖果,作為獲勝者的獎品。為了做到這一點,她做了n個動作。第一個動作是放一顆糖到盒子裡。對於剩下的每一步,她可以從兩個選項中選擇:第一個選項,如果盒子裡至少有一顆糖,那就是拿出一顆糖吃了。這樣盒子裡糖果的數量減少了1;第二種選擇是在盒子裡放糖果。在這種情況下,Alya會比之前多放1個糖果。因此,如果盒子是空的,那麼她只能使用第二個選項。

已知有n次行動和最終剩下k個糖果,問Alya吃了幾個糖果:

題解:列公式計算--設吃了x個糖果,向盒子裡放了y次,根據等差公式,y符合:

y+(y*(y-1))/2,即總共放了多少顆糖果;

所以可得到:1. x+y=n ;2.y+(y*(y-1))/2-x=k;

兩式相加得 :y*y+3*y=2*(n+k) 求出y,所以x=n-y;

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll n,k;
    scanf("%lld %lld",&n,&k);
    ll i;
    for(i=0; ;i++)
    {
        if((i*i+3*i)==(2*(n+k)))
        {
            cout<<n-i<<endl;
            break;
        }
    }
}

B - B

Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a stringsconsisting only of lowercase and uppercase Latin letters.

LetAbe a set of positions in the string. Let's call itprettyif following conditions are met:

  • letters on positions fromAin the string are all distinct and lowercase;
  • there are no uppercase letters in the string which are situated between positions fromA(i.e. there is no suchjthats[j]is an uppercase letter, anda1 < j < a2for somea1anda2fromA).

Write a program that will determine the maximum number of elements in aprettyset of positions.

Input

The first line contains a single integern(1 ≤ n ≤ 200) — length of strings.

The second line contains a stringsconsisting of lowercase and uppercase Latin letters.

Output

Print maximum number of elements inprettyset of positions for strings.

Examples

Input
11
aaaaBaabAbA
Output
2
Input
12
zACaAbbaazzC
Output
3
Input
3
ABC
Output
0

Note

In the first example the desired positions might be6and8or7and8. Positions6and7contain letters 'a', position8contains letter 'b'. The pair of positions1and8is not suitable because there is an uppercase letter 'B' between these position.

In the second example desired positions can be7,8and11. There are other ways to chooseprettyset consisting of three elements.

In the third example the given stringsdoes not contain any lowercase letters, so the answer is0.

題意:求連續的小寫字母中不同字母的最大數量

題解:運用set,set可以去掉重複的字母,存到一個字串數組裡,同時記錄下長度,找到最長的輸出

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
    char  s[1000];
    int n,ct=0;
    set<char>b;
    for(int i=0;i<n;i++)
    {
        if(s[i]>='a'&&s[i]<='z')
        {
            b.insert(s[i]);
        }
        else{
            if(ct<b.size())ct=b.size();
            b.clear();
        }
    }
    if(ct<b.size)ct=b.size();
    b.clear();
    cout<<ct<<endl;
}

F - F

On the Literature lesson Sergei noticed an awful injustice, it seems that some students are asked more often than others.

Seating in the class looks like a rectangle, wherenrows withmpupils in each.

The teacher asks pupils in the following order: at first, she asks all pupils from the first row in the order of their seating, then she continues to ask pupils from the next row. If the teacher asked the last row, then the direction of the poll changes, it means that she asks the previous row. The order of asking the rows looks as follows: the1-st row, the2-nd row,..., then - 1-st row, then-th row, then - 1-st row,..., the2-nd row, the1-st row, the2-nd row,...

The order of asking of pupils on the same row is always the same: the1-st pupil, the2-nd pupil,..., them-th pupil.

During the lesson the teacher managed to ask exactlykquestions from pupils in order described above. Sergei seats on thex-th row, on they-th place in the row. Sergei decided to prove to the teacher that pupils are asked irregularly, help him count three values:

  1. the maximum number of questions a particular pupil is asked,
  2. the minimum number of questions a particular pupil is asked,
  3. how many times the teacher asked Sergei.

If there is only one row in the class, then the teacher always asks children from this row.

Input

The first and the only line contains five integersn,m,k,xandy(1 ≤ n, m ≤ 100, 1 ≤ k ≤ 1018, 1 ≤ x ≤ n, 1 ≤ y ≤ m).

Output

Print three integers:

  1. the maximum number of questions a particular pupil is asked,
  2. the minimum number of questions a particular pupil is asked,
  3. how many times the teacher asked Sergei.

Examples

Input
1 3 8 1 1
Output
3 2 3
Input
4 2 9 4 2
Output
2 1 1
Input
5 5 25 4 3
Output
1 1 1
Input
100 100 1000000000000000000 100 100
Output
101010101010101 50505050505051 50505050505051

Note

The order of asking pupils in the first test:

  1. the pupil from the first row who seats at the first table, it means it is Sergei;
  2. the pupil from the first row who seats at the second table;
  3. the pupil from the first row who seats at the third table;
  4. the pupil from the first row who seats at the first table, it means it is Sergei;
  5. the pupil from the first row who seats at the second table;
  6. the pupil from the first row who seats at the third table;
  7. the pupil from the first row who seats at the first table, it means it is Sergei;
  8. the pupil from the first row who seats at the second table;

The order of asking pupils in the second test:

  1. the pupil from the first row who seats at the first table;
  2. the pupil from the first row who seats at the second table;
  3. the pupil from the second row who seats at the first table;
  4. the pupil from the second row who seats at the second table;
  5. the pupil from the third row who seats at the first table;
  6. the pupil from the third row who seats at the second table;
  7. the pupil from the fourth row who seats at the first table;
  8. the pupil from the fourth row who seats at the second table, it means it is Sergei;
  9. the pupil from the third row who seats at the first table;

題意是老師點名的順序是從第一排點到最後一排再逆序點到第一排,每排都是從第一個點到最後一個,讓你求點名最多的點了多少次,點名最少的點了多少次,和一個指定的位置點了多少次。

運用到了dp,但還不會用(來自大佬的程式碼如下)

思路:

1、首先觀察到N,M並不大,那麼我們考慮先處理掉提問次數剩餘<N*M的時候,那麼剩下的部分我們只要暴力塗提問次數即可。

2、提問一輪(從1走出去再回到1)是按照1.2.3.4....n.n-1.....2來的。

那麼就是第一行和最後一行只塗了一次(提問了一次);

中間的所有行都塗了兩次。

那麼每一輪提問都需要塗:(2*n-2)*m次。

那麼一共就塗了K/((2*n-2)*m)個整數次。

那麼剩餘部分就是K%((2*n-2)*m)。

剩餘部分暴力塗上去即可。

查詢也是暴力查詢的。

3、注意N==1的時候需要特判。

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
ll ans[150][150];
ll n,m,k,x,y;
int main()
{
    while(~scanf("%I64d%I64d%I64d%I64d%I64d",&n,&m,&k,&x,&y))
    {
        if(n==1)
        {
            ll all=k/(n*m);
            ll yu=k%(n*m);
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    if(yu>=1)
                        ans[i][j]=all+1;
                    else ans[i][j]=all;
                    yu--;
                }
            }
            ll maxn=0;
            ll minn=2000000000000000000;
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    maxn=max(maxn,ans[i][j]);
                    minn=min(minn,ans[i][j]);
                }
            }
            printf("%I64d %I64d %I64d\n",maxn,minn,ans[x][y]);
        }
        else
        {
            ll all=k/((2*n-2)*m);
            ll yu=k%((2*n-2)*m);
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(i==1||i==n)ans[i][j]=all;
                    else ans[i][j]=all*2;
                }
            }
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    if(yu>=1)
                    ans[i][j]++;
                    yu--;
                }
            }
            for(int i=n-1; i>=2; i--)
            {
                for(int j=1; j<=m; j++)
                {
                    if(yu>=1)
                    ans[i][j]++;
                    yu--;
                }
            }
            ll maxn=0;
            ll minn=2000000000000000000;
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    maxn=max(maxn,ans[i][j]);
                    minn=min(minn,ans[i][j]);
                }
            }
            printf("%I64d %I64d %I64d\n",maxn,minn,ans[x][y]);
        }
    }
}
View Code