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+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; int a[10]; int b[10]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); a[x]++; } for(int i=1;i<=n;i++) { int x; scanf("%d",&x); b[x]++; } for(int i=1;i<=5;i++) { if((a[i]+b[i])%2) return puts("-1"); } int ans=0; for(int i=1;i<=5;i++) { ans+=abs(a[i]-((a[i]+b[i])/2)); } printf("%d\n",ans/2); return 0; }
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+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; char a[110]; int flag[110]; int getnum(int len) { int num=0; for(int i=0;i<len;i++) { if(flag[i])continue; num=num*10+a[i]-'0'; } return num; } int main() { int n; scanf("%s%d",a,&n); int div=1; for(int i=1;i<=n;i++) div*=10; int x=strlen(a); int v=getnum(x); if(v%div==0)return puts("0"); int ans=0; for(int i=x-1;i>=0;i--) { if(a[i]!='0') { flag[i]=1; ans++; int v=getnum(x); if(v==0) return 0*printf("%d\n",x-1); if(v%div==0) return 0*printf("%d\n",ans); } } printf("%d\n",x-1); return 0; }
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+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; ll a[N]; ll b[N]; ll c[N]; int main() { int n,k; scanf("%d%d",&n,&k); ll sum=0; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); sum+=a[i]; } for(int i=1;i<=n;i++) scanf("%lld",&b[i]); for(int i=1;i<=n;i++) c[i]=a[i]-b[i]; sort(c+1,c+1+n); int pos=n; for(int i=1;i<=n;i++) { if(i<=k)continue; sum-=max(0LL,c[i]); } printf("%lld\n",sum); return 0; }
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+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; char a[N],b[N]; int flag[N],c[N]; int check(int x,int y) { int st=1; for(int i=1;i<=x;i++) { if(flag[i])continue; if(a[i]==b[st]) { st++; if(st>y) return 1; } } return 0; } int main() { scanf("%s%s",a+1,b+1); int x=strlen(a+1); int y=strlen(b+1); for(int i=1;i<=x;i++) scanf("%d",&c[i]); int l=0; int r=x,ans=-1; while(l<=r) { int mid=(l+r)>>1; for(int i=1;i<=mid;i++) flag[c[i]]=1; for(int i=mid+1;i<=x;i++) flag[c[i]]=0; //cout<<mid<<" "<<x<<" "<<y<<endl; //cout<<"xxxx"<<check(x,y)<<endl; if(check(x,y)) { l=mid+1; ans=mid; } else r=mid-1; } printf("%d\n",ans); return 0; }
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+10,M=1e6+10,inf=2147483647; const ll INF=1e18+10,mod=2147493647; map<string,int>mp; string a[5010][10]; int n,m; int flag[N]; int check(int beg,int k) { memset(flag,0,sizeof(flag)); for(int i=1;i<=n;i++) { if(a[i][3][0]=='0'||a[i][3][0]=='1') flag[i]=a[i][3][m-k]-'0'; else { int p,q; if(a[i][3][0]=='?') p=beg; else p=flag[mp[a[i][3]]]; if(a[i][5][0]=='?') q=beg; else q=flag[mp[a[i][5]]]; if(a[i][4][0]=='A') flag[i]=p&q; else if(a[i][4][0]=='O') flag[i]=p|q; else flag[i]=p^q; } } int sum=0; for(int i=1;i<=n;i++) sum+=flag[i]; return sum; } int out1[N]; int out2[N]; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { for(int j=1;j<=3;j++) cin>>a[i][j]; mp[a[i][1]]=i; if(a[i][3][0]=='0'||a[i][3][0]=='1')continue; for(int j=4;j<=5;j++) cin>>a[i][j]; } for(int i=1;i<=m;i++) { int p=check(0,i); int q=check(1,i); //cout<<p<<" "<<q<<endl; if(p>q) out2[i]=0,out1[i]=1; else if(p==q) out2[i]=out1[i]=0; else out2[i]=1,out1[i]=0; } for(int i=m;i>=1;i--) printf("%d",out1[i]); printf("\n"); for(int i=m;i>=1;i--) printf("%d",out2[i]); return 0; }
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