Codeforces 899

来源:互联网 发布:mac 东芝移动硬盘 编辑:程序博客网 时间:2024/06/04 18:53

899A Splitting in Teams
There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team.

The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can’t use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team.

Input
The first line contains single integer n (2 ≤ n ≤ 2·105) — the number of groups.

The second line contains a sequence of integers a1, a2, …, an (1 ≤ ai ≤ 2), where ai is the number of people in group i.

Output
Print the maximum number of teams of three people the coach can form.

Examples
input
4
1 1 2 1
output
1
input
2
2 2
output
0
input
7
2 2 2 1 1 1 1
output
3
input
3
1 1 1
output
1
Note
In the first example the coach can form one team. For example, he can take students from the first, second and fourth groups.

In the second example he can’t make a single team.

In the third example the coach can form three teams. For example, he can do this in the following way:

The first group (of two people) and the seventh group (of one person),
The second group (of two people) and the sixth group (of one person),
The third group (of two people) and the fourth group (of one person).

三个人组一个队伍,问是否能够将所有人组好队伍,两个人和一个人可以组,三个一个人可以组。

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAXL(2*1e5);int main(){    int n;    cin>>n;    int num1=0,num2=0;    for(int i=1; i<=n; i++)    {        int x;        cin>>x;        if(x==1)            num1++;        else            num2++;    }    if(num2>num1)        cout<<num1<<endl;    else if(num2==num1)        cout<<num2<<endl;    else        cout<<num2+(num1-num2)/3<<endl;}

899B Months and Years

Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.

A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.

In this problem you are given n (1 ≤ n ≤ 24) integers a1, a2, …, an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.

Input
The first line contains single integer n (1 ≤ n ≤ 24) — the number of integers.

The second line contains n integers a1, a2, …, an (28 ≤ ai ≤ 31) — the numbers you are to check.

Output
If there are several consecutive months that fit the sequence, print “YES” (without quotes). Otherwise, print “NO” (without quotes).

You can print each letter in arbitrary case (small or large).

Examples
input
4
31 31 30 31
output
Yes

input
2
30 30
output
No

input
5
29 31 30 31 30
output
Yes

input
3
31 28 30
output
No

input
3
31 31 28
output
Yes

Note
In the first example the integers can denote months July, August, September and October.

In the second example the answer is no, because there are no two consecutive months each having 30 days.

In the third example the months are: February (leap year) — March — April – May — June.

In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.

In the fifth example the months are: December — January — February (non-leap year).

给最多24个月份,判断是否是连续的月份。
注意:24个月份可最多横跨三年。

闰年29天,所以不可能有29 29的情况,那么只剩28 28,28 29,29 28(29代表闰年,28代表平年)

用字符串打个长表,直接查找是否有子串就行。

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<string>using namespace std;string str1="312831303130313130313031312831303130313130313031312831303130313130313031312931303130313130313031312831303130313130313031312831303130313130313031";int main(){    int n;    while(cin>>n)    {        string str;        for(int i=1; i<=n; i++)        {            string s;            cin>>s;            str+=s;        }        int flag=str1.find(str);        if(flag!=-1)            cout<<"YES"<<endl;        else            cout<<"NO"<<endl;    }}

899C Dividing the numbers
Petya has n integers: 1, 2, 3, …, n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible.

Help Petya to split the integers. Each of n integers should be exactly in one group.

Input
The first line contains a single integer n (2 ≤ n ≤ 60 000) — the number of integers Petya has.

Output
Print the smallest possible absolute difference in the first line.

In the second line print the size of the first group, followed by the integers in that group. You can print these integers in arbitrary order. If there are multiple answers, print any of them.

Examples
input
4
output
0
2 1 4
input
2
output
1
1 1
Note
In the first example you have to put integers 1 and 4 in the first group, and 2 and 3 in the second. This way the sum in each group is 5, and the absolute difference is 0.

In the second example there are only two integers, and since both groups should be non-empty, you have to put one integer in the first group and one in the second. This way the absolute difference of sums of integers in each group is 1.

题意就是把一堆数分成两个集合,问怎样分才能使得 每个集合所有数的和,然后做差的绝对值最小? 然后输出一个集合里的所有元素,随便输出一种情况即可。

分析:
一:当序列长度n为偶数时:
1.当n/2为偶数时,最后做差的结果一定是0;
2.当n/2为奇数时,最后做差的结果一定是1。
EX: 1 2 3 4—>1+4=2+3.
1 2 3 4 5 6—>1+6=2+5,然后将3和四分别放到不同的集合,做差取绝对值为1。
二:当序列长度n为奇数时:
1.当n/2为偶数时,最后做差的结果一定是1;
2.当n/2为奇数时,最后做差的结果一定是0。
EX:1 2 3 4 5,先将1拿出来,剩下的2 3 4 5如上分析,最后剩下1,差的绝对值必为1.
1 2 3 4 5 6 7,将1拿出,剩下2 3 4 5 6 7,2+7=3+6. 将1和4放进一个集合,5放进另一个集合,所以差必为0。

分析结束之后,剩下的就是该怎么输出集合元素了,上代码吧。

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<string>using namespace std;int main(){    int n;    cin>>n;    if(n%2==0)    {        int t=n/2;        if(t%2==0)        {            cout<<0<<endl;            cout<<t;            for(int i=1,j=n; i<t; i+=2,j-=2)            {                cout<<" "<<i<<" "<<j;            }            cout<<endl;        }        else        {            cout<<1<<endl;            cout<<t;            for(int i=1,j=n; i<=t; i+=2,j-=2)            {                if(i!=t)                    cout<<" "<<i<<" "<<j;                else                    cout<<" "<<i<<endl;            }        }    }    else    {        int t=n/2;        if(t%2!=0)        {            cout<<0<<endl;            cout<<t+1;            for(int i=2,j=n; i<=t+1; i+=2,j-=2)            {                if(i!=t+1)                    cout<<" "<<i<<" "<<j;                else                    cout<<" "<<i<<" "<<1;            }        }        else        {            cout<<1<<endl;            cout<<t+1;            for(int i=2,j=n; i<=t+1; i+=2,j-=2)                cout<<" "<<i<<" "<<j;            cout<<" "<<1<<endl;        }    }}
原创粉丝点击