A. Pupils Redistribution
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

In Berland each high school student is characterized by academic performance — integer value between 1 and 5.

In high school 0xFF there are two groups of pupils: the group A and the group B. Each group consists of exactly n students. An academic performance of each student is known — integer value between 1 and 5.

The school director wants to redistribute students between groups so that each of the two groups has the same number of students whose academic performance is equal to 1, the same number of students whose academic performance is 2 and so on. In other words, the purpose of the school director is to change the composition of groups, so that for each value of academic performance the numbers of students in both groups are equal.

To achieve this, there is a plan to produce a series of exchanges of students between groups. During the single exchange the director selects one student from the class A and one student of class B. After that, they both change their groups.

Print the least number of exchanges, in order to achieve the desired equal numbers of students for each academic performance.

Input

The first line of the input contains integer number n (1 ≤ n ≤ 100) — number of students in both groups.

The second line contains sequence of integer numbers a1, a2, ..., an (1 ≤ ai ≤ 5), where ai is academic performance of the i-th student of the group A.

The third line contains sequence of integer numbers b1, b2, ..., bn (1 ≤ bi ≤ 5), where bi is academic performance of the i-th student of the group B.

Output

Print the required minimum number of exchanges or -1, if the desired distribution of students can not be obtained.

Examples
Input
4
5 4 4 4
5 5 4 5
Output
1
Input
6
1 1 1 1 1 1
5 5 5 5 5 5
Output
3
Input
1
5
3
Output
-1
Input
9
3 2 5 5 2 3 3 3 2
4 1 4 1 1 2 4 4 1
Output
4

题意:一个n,表示一个班n个人,两个班,分数分成1-5,使用最少的交换顺序,使得两个班相同分数的人相同;

思路:标记一下,如果两个班合起来,一个班的分数为奇数则-1,否则abs(a[i]-((a[i]+b[i])/2));

   重复之后ans/2;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
int a[];
int b[];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
a[x]++;
}
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
b[x]++;
}
for(int i=;i<=;i++)
{
if((a[i]+b[i])%)
return puts("-1");
}
int ans=;
for(int i=;i<=;i++)
{
ans+=abs(a[i]-((a[i]+b[i])/));
}
printf("%d\n",ans/);
return ;
}
B. Weird Rounding
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k.

In the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by 10k. For example, if k = 3, in the number 30020 it is enough to delete a single digit (2). In this case, the result is 3000 that is divisible by 103 = 1000.

Write a program that prints the minimum number of digits to be deleted from the given integer number n, so that the result is divisible by 10k. The result should not start with the unnecessary leading zero (i.e., zero can start only the number 0, which is required to be written as exactly one digit).

It is guaranteed that the answer exists.

Input

The only line of the input contains two integer numbers n and k (0 ≤ n ≤ 2 000 000 000, 1 ≤ k ≤ 9).

It is guaranteed that the answer exists. All numbers in the input are written in traditional notation of integers, that is, without any extra leading zeros.

Output

Print w — the required minimal number of digits to erase. After removing the appropriate w digits from the number n, the result should have a value that is divisible by 10k. The result can start with digit 0 in the single case (the result is zero and written by exactly the only digit 0).

Examples
Input
30020 3
Output
1
Input
100 9
Output
2
Input
10203049 2
Output
3
Note

In the example 2 you can remove two digits: 1 and any 0. The result is number 0 which is divisible by any number.

题意:给你一个n,求删掉的数字最少是的n%(10^k)==0;

思路:从后往前扫,不为0就删掉,注意0的情况即可;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
char a[];
int flag[];
int getnum(int len)
{
int num=;
for(int i=;i<len;i++)
{
if(flag[i])continue;
num=num*+a[i]-'';
}
return num;
}
int main()
{
int n;
scanf("%s%d",a,&n);
int div=;
for(int i=;i<=n;i++)
div*=;
int x=strlen(a);
int v=getnum(x);
if(v%div==)return puts("");
int ans=;
for(int i=x-;i>=;i--)
{
if(a[i]!='')
{
flag[i]=;
ans++;
int v=getnum(x);
if(v==)
return *printf("%d\n",x-);
if(v%div==)
return *printf("%d\n",ans);
}
}
printf("%d\n",x-);
return ;
}
C. Dishonest Sellers
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi.

Not all of sellers are honest, so now some products could be more expensive than after a week of discounts.

Igor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items.

Input

In the first line there are two positive integer numbers n and k (1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — total number of items to buy and minimal number of items Igor wants to by right now.

The second line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 104) — prices of items during discounts (i.e. right now).

The third line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104) — prices of items after discounts (i.e. after a week).

Output

Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.

Examples
Input
3 1
5 4 6
3 1 5
Output
10
Input
5 3
3 4 7 10 3
4 5 5 12 5
Output
25
Note

In the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6 + 3 + 1 = 10.

In the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3 + 4 + 10 + 3 + 5 = 25.

题意:给你n个物品,k表示你现在最少买k个东西;

     你现在的价格是a,一个星期后的价格是b,求最少的钱购买n个东西;

思路:按差价排序即可;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
ll a[N];
ll b[N];
ll c[N];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
ll sum=;
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
}
for(int i=;i<=n;i++)
scanf("%lld",&b[i]);
for(int i=;i<=n;i++)
c[i]=a[i]-b[i];
sort(c+,c++n);
int pos=n;
for(int i=;i<=n;i++)
{
if(i<=k)continue;
sum-=max(0LL,c[i]);
}
printf("%lld\n",sum);
return ;
}
D. String Game
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.

Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" "nastya" "nastya" "nastya" "nastya" "nastya" "nastya".

Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.

It is guaranteed that the word p can be obtained by removing the letters from word t.

Input

The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.

Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct).

Output

Print a single integer number, the maximum number of letters that Nastya can remove.

Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note

In the first sample test sequence of removing made by Nastya looks like this:

"ababcba" "ababcba" "ababcba" "ababcba"

Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".

So, Nastya will remove only three letters.

题意:给你一个字符串p,一个字符串t,给你p长度的数组a,按a给你位置开始删除p中的字符

   求最多删除多少个,使得p中还有b的子串;

思路:二分数组a,check,p中是否还有即可;注意0的情况;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
char a[N],b[N];
int flag[N],c[N];
int check(int x,int y)
{
int st=;
for(int i=;i<=x;i++)
{
if(flag[i])continue;
if(a[i]==b[st])
{
st++;
if(st>y)
return ;
}
}
return ;
}
int main()
{
scanf("%s%s",a+,b+);
int x=strlen(a+);
int y=strlen(b+);
for(int i=;i<=x;i++)
scanf("%d",&c[i]);
int l=;
int r=x,ans=-;
while(l<=r)
{
int mid=(l+r)>>;
for(int i=;i<=mid;i++)
flag[c[i]]=;
for(int i=mid+;i<=x;i++)
flag[c[i]]=;
//cout<<mid<<" "<<x<<" "<<y<<endl;
//cout<<"xxxx"<<check(x,y)<<endl;
if(check(x,y))
{
l=mid+;
ans=mid;
}
else
r=mid-;
}
printf("%d\n",ans);
return ;
}
E. Bitwise Formula
time limit per test

3 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.

Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.

Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.

Input

The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).

The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of:

  1. Binary number of exactly m bits.
  2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.

Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.

Output

In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.

Examples
Input
3 3
a := 101
b := 011
c := ? XOR b
Output
011
100
Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output
0
0
Note

In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15.

For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.

题意:给你n个变量和表达式,每个数为m位,一个数?,求所有数字相加最小的答案,和最大的答案,输出?;

思路:按位枚举,暴力求最后的值,比较一下即可;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=5e3+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
map<string,int>mp;
string a[][];
int n,m;
int flag[N];
int check(int beg,int k)
{
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++)
{
if(a[i][][]==''||a[i][][]=='')
flag[i]=a[i][][m-k]-'';
else
{
int p,q;
if(a[i][][]=='?')
p=beg;
else
p=flag[mp[a[i][]]];
if(a[i][][]=='?')
q=beg;
else
q=flag[mp[a[i][]]];
if(a[i][][]=='A')
flag[i]=p&q;
else if(a[i][][]=='O')
flag[i]=p|q;
else
flag[i]=p^q;
}
}
int sum=;
for(int i=;i<=n;i++)
sum+=flag[i];
return sum;
}
int out1[N];
int out2[N];
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
for(int j=;j<=;j++)
cin>>a[i][j];
mp[a[i][]]=i;
if(a[i][][]==''||a[i][][]=='')continue;
for(int j=;j<=;j++)
cin>>a[i][j];
}
for(int i=;i<=m;i++)
{
int p=check(,i);
int q=check(,i);
//cout<<p<<" "<<q<<endl;
if(p>q)
out2[i]=,out1[i]=;
else if(p==q)
out2[i]=out1[i]=;
else
out2[i]=,out1[i]=;
}
for(int i=m;i>=;i--)
printf("%d",out1[i]);
printf("\n");
for(int i=m;i>=;i--)
printf("%d",out2[i]);
return ;
}
A. Pupils Redistribution
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

In Berland each high school student is characterized by academic performance — integer value between 1 and 5.

In high school 0xFF there are two groups of pupils: the group A and the group B. Each group consists of exactly n students. An academic performance of each student is known — integer value between 1 and 5.

The school director wants to redistribute students between groups so that each of the two groups has the same number of students whose academic performance is equal to 1, the same number of students whose academic performance is 2 and so on. In other words, the purpose of the school director is to change the composition of groups, so that for each value of academic performance the numbers of students in both groups are equal.

To achieve this, there is a plan to produce a series of exchanges of students between groups. During the single exchange the director selects one student from the class A and one student of class B. After that, they both change their groups.

Print the least number of exchanges, in order to achieve the desired equal numbers of students for each academic performance.

Input

The first line of the input contains integer number n (1 ≤ n ≤ 100) — number of students in both groups.

The second line contains sequence of integer numbers a1, a2, ..., an (1 ≤ ai ≤ 5), where ai is academic performance of the i-th student of the group A.

The third line contains sequence of integer numbers b1, b2, ..., bn (1 ≤ bi ≤ 5), where bi is academic performance of the i-th student of the group B.

Output

Print the required minimum number of exchanges or -1, if the desired distribution of students can not be obtained.

Examples
Input
4
5 4 4 4
5 5 4 5
Output
1
Input
6
1 1 1 1 1 1
5 5 5 5 5 5
Output
3
Input
1
5
3
Output
-1
Input
9
3 2 5 5 2 3 3 3 2
4 1 4 1 1 2 4 4 1
Output
4

Codeforces Round #402 (Div. 2) A,B,C,D,E的更多相关文章

  1. Codeforces Round #402 (Div. 2)

    Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...

  2. Codeforces Round #402 (Div. 2) A+B+C+D

    Codeforces Round #402 (Div. 2) A. Pupils Redistribution 模拟大法好.两个数列分别含有n个数x(1<=x<=5) .现在要求交换一些数 ...

  3. Codeforces Round #402 (Div. 2) D. String Game

    D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  4. Codeforces Round #402 (Div. 2) A B C sort D二分 (水)

    A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. 【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding

    暴搜 #include<cstdio> #include<algorithm> using namespace std; int n,K,Div=1,a[21],m,ans=1 ...

  6. Codeforces Round #402 (Div. 2) 题解

    Problem A: 题目大意: 给定两个数列\(a,b\),一次操作可以交换分别\(a,b\)数列中的任意一对数.求最少的交换次数使得任意一个数都在两个序列中出现相同的次数. (\(1 \leq a ...

  7. Codeforces Round #402 (Div. 2) 阵亡记

    好长时间没有打Codeforces了,今天被ysf拉过去打了一场. lrd也来参(nian)加(ya)比(zhong)赛(sheng) Problem A: 我去,这不SB题吗.. 用桶统计一下每个数 ...

  8. CodeForces Round #402 (Div.2) A-E

    2017.2.26 CF D2 402 这次状态还算能忍吧……一路不紧不慢切了前ABC(不紧不慢已经是在作死了),卡在D,然后跑去看E和F——卧槽怎么还有F,早知道前面做快点了…… F看了看,不会,弃 ...

  9. Codeforces Round #402 (Div. 2) B

    Description Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k. ...

随机推荐

  1. 从零打造在线网盘系统之Hibernate配置O/R映射

    欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...

  2. python安装whl文件的注意事项(windows系统)

    首先给大家来一波福利,在没有连接外网(互联网)的情况下,只有公司内网或者断网情况下,需要安装python的一些依赖,不会操作的同学可能就会遇到麻烦.这里教大家离线安装python依赖. 方法:使用.w ...

  3. 利用Qt开发跨平台APP(二)(iOS,使用Qt5.9,很详细,有截图)

    本文将手把手教你如何使用Qt编译出iOS应用程序. Qt是一个优秀的跨平台开发工具.我们利用Qt可以很方便地将一次编写的应用,多次编译到不同平台上,如Windows.Linux.MAC.Android ...

  4. IOS使用Core-Plot画折线图

    关于Core-Plot的配置.大家能够參考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权全部.转载请注明原文转自:http://blog.csdn.net/ ...

  5. Openstack(九)部署nova服务(控制节点)

    9.1nova服务介绍 nova是openstack最早的组件之一,nova分为控制节点和计算节点,计算节点通过nova computer进行虚拟机创建,通过libvirt调用kvm创建虚拟机,nov ...

  6. mybatis 中jdbctype和javatype的对应关系

    1:mybatis 中jdbctype和javatype的对应关系 JDBC Type Java Type CHAR String VARCHAR String LONGVARCHAR String ...

  7. Checkpoint的运行原理和源码实现

    引言 Checkpoint 到底是什么和需要用 Checkpoint 解决什么问题: Spark 在生产环境下经常会面临 Transformation 的 RDD 非常多(例如一个Job 中包含1万个 ...

  8. Spring boot 开发WebService遇到的问题之一

    当pom.xml文件中的配置: <artifactId>spring-boot-starter-parent</artifactId><version>2.0.6. ...

  9. Java用数据结构解决实现问题之数学问题

    有趣的整数: 完数:如果一个数字恰好等于他的因子之和,就叫做完数,需求是求出10000以内的所有的完数. 解法:1.用n去除以1-n之间的所有的整数,将能整除的被除数保存到一个数组中,作为n的一个因子 ...

  10. cookie—基于js的coolie使用

    //设置cookie function setCookie(name,value) { var Days = 30; var exp = new Date(); exp.setTime(exp.get ...