Codeforces Round #402 (Div. 2) A B C sort D二分 (水)
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的数字 每个数字的范围为[1,5] 现在要求两个组内 [1,5]各个数字出现的次数相同 问最少要交换两个组内的数字多少次.
题解:若某一个数字在两组内出现的次数之和为奇数 则无论怎么交换都无法满足要求,输出“-1” 其他情况下题目都会有解
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
int n;
int a[];
int b[];
int c[];
int x;
int main()
{
int ans1=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
a[x]++;
c[x]++;
}
for(int i=;i<=n;i++)
{
scanf("%d",&x);
b[x]++;
c[x]++;
}
for(int i=;i<=;i++)
{
if(c[i]%)
{
printf("-1\n");
return ;
}
else{
if(a[i]>b[i])
{
ans1+=a[i]-c[i]/;
}
}
}
printf("%d\n",ans1);
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 wdigits 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,k 问最少要移除n中的多少位数字使得能够除尽10^k 问题一定有解
题解:从后向前遍历 记录零与非零的个数zha,ans 直到zha等于k 则输出ans
否则 输出n的长度减一
0能够除尽任何数
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
ll k;
char a[];
int main()
{
scanf("%s %I64d",a,&k);
int ans=;
int len=strlen(a);
int zha=;
if(len>=k)
{
for(int i=len-;i>=;i--)
{
if(a[i]!='')
ans++;
else
zha++;
if(zha==k)
{
printf("%d\n",ans);
return ;
}
}
printf("%d\n",len-);
return ;
}
else
{
printf("%d\n",len-);
}
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的物品 问购买所有的n个物品 最少的花费。
题解:sort
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
int n,k;
struct node
{
int a,b;
int cha;
}N[];
bool cmp(struct node aa,struct node bb)
{
return aa.cha<bb.cha;
}
int main()
{
int ans=;
scanf("%d %d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&N[i].a);
}
for(int i=;i<=n;i++)
{
scanf("%d",&N[i].b);
N[i].cha=N[i].a-N[i].b;
}
sort(N+,N++n,cmp);
for(int i=;i<=k;i++)
{
ans+=N[i].a;
}
for(int i=k+;i<=n;i++)
{
if(N[i].cha<)
ans+=N[i].a;
else
ans+=N[i].b;
}
printf("%d\n",ans);
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.
题意:给你初始字符串 目标字符串 以及对初始字符串执行删除操作的顺序
若对某个字符删除之后无法到达目标状态 则无法删除 并且结束
问最多能删除多少个字符
题解:二分答案 fun()
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
char a[];
char b[];
int c[];
map<int,int> mp;
int len1,len2;
bool fun(int x)
{
mp.clear();
for(int i=;i<=x;i++)
{
mp[c[i]-]=;
}
int exm=;
int jishu=;
for(int j=;j<len2;j++)
{
while(exm<len1)
{
if(a[exm]==b[j]&&mp[exm]==){
jishu++;
exm++;
break;
}
else
exm++;
}
}
if(jishu==len2)
return true;
else
return false;
}
int main()
{
scanf("%s",a);
scanf("%s",b);
len1=strlen(a);
len2=strlen(b);
for(int i=;i<len1;i++)
scanf("%d",&c[i]);
int l=,r=len1-,mid;
while(l<=r)
{
mid=(l+r)/;
if(fun(mid))
{
l=mid+;
}
else
{
r=mid-;
}
}
printf("%d\n",r+);
return ;
}
Codeforces Round #402 (Div. 2) A B C sort D二分 (水)的更多相关文章
- 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) A,B,C,D,E
A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 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 ...
- 【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. ...
随机推荐
- 解决ssh_exchange_identification:read connection reset by peer 原因
服务器改了密码,试过密码多次后出现: ssh_exchange_identification: read: Connection reset by peer 可以通过ssh -v查看连接时详情 Ope ...
- JAVA基础学习之路(三)类定义及构造方法
类的定义及使用 一,类的定义 class Book {//定义一个类 int price;//定义一个属性 int num; public static int getMonney(int price ...
- 【递归入门】组合+判断素数:dfs(递归)
题目描述 已知 n 个整数b1,b2,…,bn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和. 例如当 n=4,k=3,4 个整数分别为 3,7,12, ...
- Python3 标准库:sys
import sys print(sys.argv[0]) print(sys.argv[1]) print(len(sys.argv)) print(str(sys.argv)) print(sys ...
- 自测之Lesson6:文件I/O
题目:区分文件I/O和标准I/O. 区别: ①首先两者一个显著的不同点在于,标准I/O默认采用了缓冲机制,比如调用fopen函数,不仅打开一个文件,而且建立了一个缓冲区(读写模式下将建立两个缓冲区), ...
- CNN之间的计算
参考内容 1.网易云课堂微专业——深度学习—04第一周:http://mooc.study.163.com/smartSpec/detail/1001319001.htm 2.CNN基础介绍:http ...
- lintcode-181-将整数A转换为B
181-将整数A转换为B 如果要将整数A转换为B,需要改变多少个bit位? 注意事项 Both n and m are 32-bit integers. 样例 如把31转换为14,需要改变2个bit位 ...
- intellij idea 之 CheckStyle 代码格式校验
- tomcat web页面管理应用配置
大部分时候,我们的tomcat服务器都不是部署在本机,那么怎么样不通过ftp/sftp方式来将war包部署到tomcat容器呢? tomcat有提供web页面管理应用的功能. 我们来看看怎么配置实现该 ...
- 查询MySQL某字段相同值得重复数据
1.先查询重复的id: SELECT book_id,COUNT(*) AS COUNT FROM xs_book_source WHERE site_id=5 GROUP BY book_id HA ...