A. Drinks Choosing

                                                                                                             time limit per test:2 seconds
                                                                                                       memory limit per test:256 megabytes
                                                                                                                     input:standard input
                                                                                                                     output:standard output

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
input

Copy
5 3
1
3
1
1
2
output

Copy
4
input

Copy
10 3
2
1
3
2
3
3
1
3
1
2
output

Copy
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种类型的饮料,每个学生喜欢的饮料已列举出来,第一步判断学生人数的奇偶确定饮料组数,然后用数组存取读入的数字,每当存取2个同种类型的饮料饮料组数-1,ans+=2,a[i]清0

一个for循环,若此时饮料组数为0,结束循环,反之,饮料组数--,ans++,最后输出答案ans

 #include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + ;
int main()
{
ios::sync_with_stdio(false);
cin.tie(); cout.tie();
int n,k;
int a[N];
memset(a,,sizeof(a));
cin>>n>>k;
int re = (n / ) + (n & );
int ans = ;
for(int i = ; i <= n; i++)
{
int num;
cin>>num;
a[num]++;
if(a[num] == && re)
{
ans += ;
re--;
a[num] = ;
}
}
for(int i = ; i <= k; i++)
{
if(!re)
break;
if(a[i])
{
re--;
ans++;
}
}
cout<<ans<<endl;
return ;
}

B. Sport Mafia

                                                                                                   time limit per test:2 seconds
                                                                                             memory limit per test:256 megabytes
                                                                                                           input:standard input
                                                                                                         output:standard output

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 performs nn actions. 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 take exactly one candy out and eat it. This way the number of candies in the box decreased by 11;
  • the second option is to put candies in the box. In this case, Alya will put 11 more 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 perform 99 actions, the number of candies at the end will be 1111, while Alya will eat 44 candies in total.

You know the total number of actions nn and the number of candies at the end kk. 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 given nn and kk the 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 integers nn and kk (1≤n≤1091≤n≤109; 0≤k≤1090≤k≤109) — the total number of moves and the number of candies in the box at the end.

It's guaranteed, that for the given nn and kk the 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

Copy
1 1
output

Copy
0
input

Copy
9 11
output

Copy
4
input

Copy
5 0
output

Copy
3
input

Copy
3 2
output

Copy
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 ate 00 candies.

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

  • put 11 candy,
  • put 22 candies,
  • eat a candy,
  • eat a candy,
  • put 33 candies,
  • eat a candy,
  • put 44 candies,
  • eat a candy,
  • put 55 candies.

This way, she will make exactly n=9n=9 actions and in the end the box will contain 1+2−1−1+3−1+4−1+5=111+2−1−1+3−1+4−1+5=11 candies. The answer is 44, since she ate 44 candies in total.

题解:按照上述题干说的模拟,你有两个选择:第一:你可以吃掉一个,然后盒子里的糕点减一;第二:你可以放入糕点到盒子里去,但是每次放的比前一次的要多放一个。

 #include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(); cout.tie();
int n,k;
cin>>n>>k;
int ans = , sum = ,num = ;
while(n--)
{
if(sum <= k)
{
num++;
sum += num;
}
else if(sum > k)
{
sum--;
ans++;
}
}
cout<<ans<<endl;
return ;
}

                                                                                                               C. Basketball Exercise

                                                                                                              time limit per test:2 seconds
                                                                                                        memory limit per test:256 megabytes
                                                                                                                                  input:standard input
                                                                                                                                output:standard output

Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. 2⋅n2⋅n students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly nn people in each row). Students are numbered from 11 to nn in each row in order from left to right.

Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all 2n2n students (there are no additional constraints), and a team can consist of any number of students.

Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.

Input

The first line of the input contains a single integer nn (1≤n≤1051≤n≤105) — the number of students in each row.

The second line of the input contains nn integers h1,1,h1,2,…,h1,nh1,1,h1,2,…,h1,n (1≤h1,i≤1091≤h1,i≤109), where h1,ih1,i is the height of the ii-th student in the first row.

The third line of the input contains nn integers h2,1,h2,2,…,h2,nh2,1,h2,2,…,h2,n (1≤h2,i≤1091≤h2,i≤109), where h2,ih2,i is the height of the ii-th student in the second row.

Output

Print a single integer — the maximum possible total height of players in a team Demid can choose.

Examples
input

Copy
5
9 3 5 7 3
5 8 1 4 5
output

Copy
29
input

Copy
3
1 2 9
10 1 1
output

Copy
19
input

Copy
1
7
4
output

Copy
7
Note

In the first example Demid can choose the following team as follows:

In the second example Demid can choose the following team as follows:

题意:总共有2n个人,第一排和第二排都是编号从1到n,现在让你选择任意多个人使身高总和最大,条件限制同一个编号只能有一个人,编号相邻的人不可以在同一排。

题解:1.确定状态:dp[i][j]表示编号为i的人选择状态为j时的最大身高。

           2.确定状态转移方程:dp[i][j] = dp[i][j] + max(dp[i+1][j-2],dp[i+1][j-1])

           3.确定编程实现方式:for(ll i = 2; i <= n; i++)

                                              {

                                                   dp[1][i] += max(dp[2][i-2],dp[2][i-1]);

                                                   dp[2][i] += max(dp[1][i-2],dp[1][i-1]);

                                               }

                                               cout<<max(dp[1][n],dp[2][n])<<endl;

 #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + ;
ll dp[][N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(); cout.tie();
int n;
cin>>n;
for(int i = ; i <= n; i++)
cin>>dp[][i];
for(int i = ; i <= n; i++)
cin>>dp[][i];
for(int i = ; i <= n; i++)
{
dp[][i] += max(dp[][i-],dp[][i-]);
dp[][i] += max(dp[][i-],dp[][i-]);
}
cout<<max(dp[][n],dp[][n])<<endl;
return ;
}

优化后的AC代码:

 #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + ;
ll dp[][N];
ll val[][N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(); cout.tie();
int n;
cin>>n;
for(int i = ; i <= n; i++)
cin>>val[][i];
for(int i = ; i <= n; i++)
cin>>val[][i];
for(int i = ; i <= n; i++)
for(int j = ; j <=; j++)
dp[j][i] = max(dp[j][i-],dp[-j][i-]+val[j][i]);
cout<<max(dp[][n],dp[][n])<<endl;
return ;
}

Codeforce Round #574(Div.2)的更多相关文章

  1. codeforce round #467(div.2)

    A. Olympiad 给出n个数,让你找出有几个非零并且不重复的数 所以用stl的set //#define debug #include<stdio.h> #include<ma ...

  2. codeforce round#466(div.2)C. Phone Numbers

    C. Phone Numbers time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...

  3. codeforce round#466(div.2) B. Our Tanya is Crying Out Loud

    B. Our Tanya is Crying Out Loud time limit per test1 second memory limit per test256 megabytes input ...

  4. Codeforce Round #555 Div.3 D - N Problems During K Days

    构造题 话说挺水的题..当时怎么就WA到自闭呢.. 先把每个位置按照最低要求填满,也就是相差1..然后从最后一位开始把剩下的数加上,直到不能加为止. #include <bits/stdc++. ...

  5. Codeforce Round #554 Div.2 C - Neko does Maths

    数论 gcd 看到这个题其实知道应该是和(a+k)(b+k)/gcd(a+k,b+k)有关,但是之后推了半天,思路全无. 然而..有一个引理: gcd(a, b) = gcd(a, b - a) = ...

  6. Codeforce Round #554 Div.2 D - Neko and Aki's Prank

    dp 找规律 我好菜啊好菜啊,完全没有思路. 在合法的括号序列中,左括号数一定大于等于右括号数的,所以我们可以先定义平衡度为左括号数-右括号数. 然后可以发现一个惊人的规律..就是在trie同一深度上 ...

  7. 「日常训练」Skills(Codeforce Round #339 Div.2 D)

    题意(CodeForces 614D) 每个人有\(n(n\le 10^5)\)个技能,技能等级都在\([0,10^9]\)的范围,每个技能有一个当前等级,所有技能的最高等级都为A.一个人的力量被记做 ...

  8. 「知识学习&日常训练」莫队算法(一)(Codeforce Round #340 Div.2 E)

    题意 (CodeForces 617E) 已知一个长度为\(n\)的整数数列\(a[1],a[2],-,a[n]\),给定查询参数\(l,r\),问\([l,r]\)内,有多少连续子段满足异或和等于\ ...

  9. Codeforces Round #574 (Div. 2)——C. Basketball Exercise(简单DP)

    题目传送门 题意: 输入n,给出两组均为 n个数字的数组a和b,轮流从a和b数组中取出一个数字,要求严格按照当前所选数字的数组下标比上一个所选数字的数组下标更大,计算能够取出的数字加起来的总和最大能为 ...

随机推荐

  1. sift、surf、orb 特征提取及最优特征点匹配

    目录 sift sift特征简介 sift特征提取步骤 surf surf特征简介 surf特征提取步骤 orb orb特征简介 orb特征提取算法 代码实现 特征提取 特征匹配 附录 sift si ...

  2. Docker笔记(七):常用服务安装——Nginx、MySql、Redis

    开发中经常需要安装一些常用的服务软件,如Nginx.MySql.Redis等,如果按照普通的安装方法,一般都相对比较繁琐 —— 要经过下载软件或源码包,编译安装,配置,启动等步骤,使用 Docker ...

  3. 02.Mybatis的动态代理方式实现增删改查

    动态代理的方式实现增删改查: 通过约定的方式定位sql语句 约定 > 配置文件 > 硬编码 约定的目标是省略掉通过硬编码的方式定位sql的代码,通过接口直接定位出sql语句,以下代码为通过 ...

  4. DedeCMS V5.7 SP2前台文件上传漏洞(CVE-2018-20129)

    DedeCMS V5.7 SP2前台文件上传漏洞(CVE-2018-20129) 一.漏洞描述 织梦内容管理系统(Dedecms)是一款PHP开源网站管理系统.Dedecms V5.7 SP2版本中的 ...

  5. python3学习-logging模块

    1.logging模块的使用非常简单,引入模块就可以使用. import logging logging.debug('This is debug message') logging.info('Th ...

  6. react-navigation报错

    用react-navigation配置路由时,出现如下报错或白屏. 我的代码原来是 import {AppRegistry} from 'react-native'; import App from ...

  7. SVN服务器更改ip地址客户端怎么设置

    SVN 服务器 IP 地址修改后,客户端对服务器的连接可以采用以下的方法重定位: 1. 如果客户端工具是TortoiseSVN,直接在工作副本上右键,选择TortoiseSVN->relocat ...

  8. memcached中hash表相关操作

      以下转自http://blog.csdn.net/luotuo44/article/details/42773231 memcached源码中assoc.c文件里面的代码是构造一个哈希表.memc ...

  9. Springboot 优雅停止服务的几种方法

    在使用Springboot的时候,都要涉及到服务的停止和启动,当我们停止服务的时候,很多时候大家都是kill -9 直接把程序进程杀掉,这样程序不会执行优雅的关闭.而且一些没有执行完的程序就会直接退出 ...

  10. 前端利器躬行记(1)——npm

    npm(Node Package Manager)是Node.js的包管理工具,相当于一个在线仓库.它提供了一个公共的平台,将分散在世界各地的包集中起来,能轻松的安装.分享和管理相关的包,不用再为搜索 ...