Codeforces Round #402 (Div. 2) A,B,C,D,E
1 second
256 megabytes
standard input
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.
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.
Print the required minimum number of exchanges or -1, if the desired distribution of students can not be obtained.
4
5 4 4 4
5 5 4 5
1
6
1 1 1 1 1 1
5 5 5 5 5 5
3
1
5
3
-1
9
3 2 5 5 2 3 3 3 2
4 1 4 1 1 2 4 4 1
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 ;
}
1 second
256 megabytes
standard input
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.
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.
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).
30020 3
1
100 9
2
10203049 2
3
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 ;
}
2 seconds
256 megabytes
standard input
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.
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).
Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.
3 1
5 4 6
3 1 5
10
5 3
3 4 7 10 3
4 5 5 12 5
25
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 ;
}
2 seconds
512 megabytes
standard input
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.
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).
Print a single integer number, the maximum number of letters that Nastya can remove.
ababcba
abb
5 3 4 1 7 6 2
3
bbbabb
bb
1 6 3 4 2 5
4
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 ;
}
3 seconds
512 megabytes
standard input
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.
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:
- Binary number of exactly m bits.
- 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.
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.
3 3
a := 101
b := 011
c := ? XOR b
011
100
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
0
0
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 ;
}
1 second
256 megabytes
standard input
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.
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.
Print the required minimum number of exchanges or -1, if the desired distribution of students can not be obtained.
4
5 4 4 4
5 5 4 5
1
6
1 1 1 1 1 1
5 5 5 5 5 5
3
1
5
3
-1
9
3 2 5 5 2 3 3 3 2
4 1 4 1 1 2 4 4 1
4
Codeforces Round #402 (Div. 2) A,B,C,D,E的更多相关文章
- Codeforces Round #402 (Div. 2)
Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...
- Codeforces Round #402 (Div. 2) A+B+C+D
Codeforces Round #402 (Div. 2) A. Pupils Redistribution 模拟大法好.两个数列分别含有n个数x(1<=x<=5) .现在要求交换一些数 ...
- 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 ...
- 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 ...
- 【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 ...
- Codeforces Round #402 (Div. 2) 题解
Problem A: 题目大意: 给定两个数列\(a,b\),一次操作可以交换分别\(a,b\)数列中的任意一对数.求最少的交换次数使得任意一个数都在两个序列中出现相同的次数. (\(1 \leq a ...
- Codeforces Round #402 (Div. 2) 阵亡记
好长时间没有打Codeforces了,今天被ysf拉过去打了一场. lrd也来参(nian)加(ya)比(zhong)赛(sheng) Problem A: 我去,这不SB题吗.. 用桶统计一下每个数 ...
- CodeForces Round #402 (Div.2) A-E
2017.2.26 CF D2 402 这次状态还算能忍吧……一路不紧不慢切了前ABC(不紧不慢已经是在作死了),卡在D,然后跑去看E和F——卧槽怎么还有F,早知道前面做快点了…… F看了看,不会,弃 ...
- Codeforces Round #402 (Div. 2) B
Description Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k. ...
随机推荐
- ThinkPHP简单结构介绍!
thinkPHP简单结构介绍: application : 应用 extend:扩展 扩展内库 public:入口文件 index.php 在里面 runtime:缓存文件(里面的文件可以随便删除) ...
- R载入需要的程辑包:rJava Error
> library(xlsx) 载入需要的程辑包:rJava Error : loadNamespace()里算'rJava'时.onLoad失败了,详细内容: 调用: inDL(x, as.l ...
- 判断手机访问还是pc访问
function isMobile(){ // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) re ...
- nginx中文乱码问题
搭建nginx成功后,在页面中写入了中文字幕,结果发现乱码 于是修改了nginx配置文件nginx.conf:如下 server { listen ; server_name example.com; ...
- 阿里云ecs禁止ping,禁止telnet
现在的中小型企业服务器大多是云比较多,因此,可能会面临着服务器ping不通,或者是端口telnet不通的情况,但是服务器上的服务仍然是正常的情况,这个时候我们就要考虑是不是云上配置了访问规则了.废话不 ...
- ruby rvm groke
https://ruby-china.org/wiki/rvm-guide http://www.cnblogs.com/peak-c/p/7338291.html https://doc.yonyo ...
- Rancher OS
Rancher OS 是生产规模中运行 Docker 最小,最简单的方式.RancherOS 的所有东西都作为 Docker 管理的容器.这些系统服务包括 udev 和 rsyslog.Rancher ...
- 设计模式之——Observer模式
Observer模式又叫做观察者模式,当观察对象状态发生变化的时候,就会通知给观察者.这种模式适用于根据对象状态进行响应的场景! 实例程序是一个输出数字的程序. 观察者Observer类用于每500m ...
- SDL结合QWidget的简单使用说明(2)
上篇主要讲了针对yv12流数据的渲染,但有时候我们显示视频还要求加一些信息,比如头像,昵称等等.一般的想法是在渲染窗口之上做一个小控件来负责: 但是很遗憾,你会发现你的控件被SDL的渲染完全遮住了,渲 ...
- 【Python】两个for循环嵌套练习
要求:取下方篮框中内容并写入csv文件中.用两个for循环将每一行取出来作为一个元素存放到新数组中. # coding=utf-8 from selenium import webdriver fro ...