赞
踩
翻译:
有一个序列n。和一个数k
你要从中挑出尽可能多的数,保证其中不存在一对数,满足一个数是另一个数的k倍。
求最多的挑出的数。
输入格式:
第一行一个数n
第二行n个数a[i]
输出格式:
一个数表示答案。
A k k k -multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k k k .
That is, there are no two integers x x x and y y y ( x < y ) (x<y) (x<y) from the set, such that y = x ⋅ k y=x·k y=x⋅k .
You’re given a set of n n n distinct positive integers.
Your task is to find the size of it’s largest k k k -multiple free subset.
The first line of the input contains two integers n n n and k k k ( 1 < = n < = 1 0 5 , 1 < = k < = 1 0 9 1<=n<=10^{5},1<=k<=10^{9} 1<=n<=105,1<=k<=109 ).
The next line contains a list of n n n distinct positive integers a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1,a2,...,an ( 1 < = a i < = 1 0 9 ) (1<=a_{i}<=10^{9}) (1<=ai<=109) .
All the numbers in the lines are separated by single spaces.
On the only line of the output print the size of the largest k k k -multiple free subset of a 1 , a 2 , . . . , a n {a_{1},a_{2},...,a_{n}} a1,a2,...,an .
6 2
2 3 6 5 4 10
3
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}.
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; typedef long long ll; ll n,k; ll a[N],ss=0; map<ll,bool> s; int main() { cin>>n>>k; for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); } sort(a+1, a + n+1); for (int i=1;i<=n;i++) { if(s[a[i]]==0){ s[a[i]*k]=1; ss++; } } cout<<ss<<endl; return 0; }
有 n n n个数字,为 a [ 1 ] . . . a [ n ] a[1]\ ...\ a[n] a[1] ... a[n], q q q个询问。
每次读入 l , r l,r l,r,询问 ∑ i = l r a [ i ] \sum_{i=l}^ra[i] ∑i=lra[i]。
这样岂不是很水!
于是你可以重新排列读入的 a a a数组,使询问的总和最大。
输出这个最大值。
n , q ≤ 2 × 1 0 5 n,q \le 2\times 10^5 n,q≤2×105
1 ≤ a i ≤ 2 × 1 0 5 1\le a_i \le 2 \times 10^5 1≤ai≤2×105
1 ≤ l i ≤ r i ≤ n 1\le l_i \le r_i \le n 1≤li≤ri≤n
The little girl loves the problems on array queries very much.
One day she came across a rather well-known problem: you’ve got an array of n n n elements (the elements of the array are indexed starting from 1); also, there are q q q queries, each one is defined by a pair of integers l i l_{i} li , r i r_{i} ri ( 1 < = l i < = r i < = n ) (1<=l_{i}<=r_{i}<=n) (1<=li<=ri<=n) .
You need to find for each query the sum of elements of the array with indexes from l i l_{i} li to r i r_{i} ri , inclusive.
The little girl found the problem rather boring.
She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible.
Your task is to find the value of this maximum sum.
The first line contains two space-separated integers n n n ( 1 < = n < = 2 ⋅ 1 0 5 1<=n<=2·10^{5} 1<=n<=2⋅105 ) and q q q ( 1 < = q < = 2 ⋅ 1 0 5 1<=q<=2·10^{5} 1<=q<=2⋅105 ) — the number of elements in the array and the number of queries, correspondingly.
The next line contains n n n space-separated integers a i a_{i} ai ( 1 < = a i < = 2 ⋅ 1 0 5 1<=a_{i}<=2·10^{5} 1<=ai<=2⋅105 ) — the array elements.
Each of the following q q q lines contains two space-separated integers l i l_{i} li and r i r_{i} ri ( 1 < = l i < = r i < = n 1<=l_{i}<=r_{i}<=n 1<=li<=ri<=n ) — the i i i -th query.
In a single line print a single integer — the maximum sum of query replies after the array elements are reordered.
Please, do not use the %lld specifier to read or write 64-bit integers in С++.
It is preferred to use the cin, cout streams or the %I64d specifier.
3 3
5 3 2
1 2
2 3
1 3
25
5 3
5 2 4 1 3
1 5
2 3
2 3
33
#include<bits/stdc++.h> using namespace std; const int N=2e5+10; typedef long long ll; ll n,q,a[N],s=0,l,r,b[N]; int main() { cin>>n>>q; for(ll i=1;i<=n;i++){ scanf("%lld",&a[i]); } sort(a+1,a+1+n); for(ll i=1;i<=q;i++){ scanf("%lld%lld",&l,&r); b[l]++; b[r+1]--; } for(ll i=1;i<=n;i++){ b[i]+=b[i-1]; } sort(b+1,b+1+n); for(ll i=1;i<=n;i++){ s+=b[i]*a[i]; } cout<<s<<endl; return 0; }
给定两个 01 01 01 串 , , , 问 A A A 是否可以通过相邻两位的异或和或操作得到 B B B 串。
异或 : 01 / 10 → 11 , 11 → 10 / 01 :01/10\to11,11\to10/01 :01/10→11,11→10/01
或 : 10 / 01 → 11 :10/01\to11 :10/01→11
The Bitlandians are quite weird people.
They do everything differently.
They have a different alphabet so they have a different definition for a string.
A Bitlandish string is a string made only of characters “0” and “1”.
BitHaval (the mayor of Bitland) loves to play with Bitlandish strings.
He takes some Bitlandish string a a a , and applies several (possibly zero) operations to it.
In one operation the mayor may take any two adjacent characters of a string, define one of them as x x x and the other one as y y y .
Then he calculates two values p p p and q q q : p = x x o r y p=x xor y p=xxory , q = x o r y q=x or y q=xory .
Then he replaces one of the two taken characters by p p p and the other one by q q q .
The x o r xor xor operation means the bitwise excluding OR operation.
The o r or or operation is the bitwise OR operation.
So for example one operation can transform string 11 to string 10 or to string 01.
String 1 cannot be transformed into any other string.
You’ve got two Bitlandish strings a a a and b b b .
Your task is to check if it is possible for BitHaval to transform string a a a to string b b b in several (possibly zero) described operations.
The first line contains Bitlandish string a a a , the second line contains Bitlandish string b b b .
The strings can have different lengths.
It is guaranteed that the given strings only consist of characters “0” and “1”.
The strings are not empty, their length doesn’t exceed 1 0 6 10^{6} 106 .
Print “YES” if a a a can be transformed into b b b , otherwise print “NO”.
Please do not print the quotes.
11
10
YES
1
01
NO
000
101
NO
#include<bits/stdc++.h> using namespace std; const int N=1e6+10; typedef long long ll; char a[N],b[N]; int n1,n2,f1,f2; int main() { scanf("%s %s",a+1,b+1); int l1=strlen(a+1),l2=strlen(b+1); if(l1!=l2){ printf("NO");return 0; } for(int i=1;i<=l1;i++){ if(a[i]=='1'){f1=1;break;} } for(int i=1;i<=l1;i++){ if(b[i]=='1'){f2=1;break;} } if((f1&&f2)||(!f1&&!f2)) printf("YES"); else printf("NO"); return 0; }
有 n n n 个蛋,A和G需要给这 n n n 个鸡蛋涂色。A涂第 i i i 个鸡蛋得到的钱为 A i A_i Ai ,G涂第 i i i 个鸡蛋得到的钱为 G i G_i Gi ,且满足 A i + G i = 1000 A_i+G_i=1000 Ai+Gi=1000 。需要把这 n n n 个鸡蛋分配给A和G,求怎样分配使得A和G得到的钱 S A S_A SA 和 S G S_G SG 的差不超过 500 500 500 。
第一行一个整数 n ( 1 ≤ n ≤ 1 0 6 ) n(1\le n\le 10^6) n(1≤n≤106) 。
接下来 n n n 行,每行两个整数 A i , G i ( 0 ≤ A i , G i ≤ 1000 , A i + G i = 1000 ) A_i,G_i(0\le A_i,G_i\le 1000,A_i+G_i=1000) Ai,Gi(0≤Ai,Gi≤1000,Ai+Gi=1000) 。
一行,长度为 n n n 的字符串,第 i i i 个字符为A或G,表示分配方案。
有多种解输出任意一种,需要满足 ∣ S A − S G ∣ ≤ 500 |S_A-S_G|\le 500 ∣SA−SG∣≤500 。
The Bitlandians are quite weird people.
They have very peculiar customs.
As is customary, Uncle J. wants to have n n n eggs painted for Bitruz (an ancient Bitland festival).
He has asked G. and A. to do the work.
The kids are excited because just as is customary, they’re going to be paid for the job!
Overall uncle J. has got n n n eggs. G. named his price for painting each egg.
Similarly, A. named his price for painting each egg.
It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000 1000 1000 .
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child.
Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500 500 500 .
Help Uncle J.
Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
The first line contains integer n n n ( 1 < = n < = 1 0 6 ) (1<=n<=10^{6}) (1<=n<=106) — the number of eggs.
Next n n n lines contain two integers a i a_{i} ai and g i g_{i} gi each ( 0 < = a i , g i < = 1000 ; a i + g i = 1000 ) (0<=a_{i},g_{i}<=1000; a_{i}+g_{i}=1000) (0<=ai,gi<=1000;ai+gi=1000) : a i a_{i} ai is the price said by A.
for the i i i -th egg and g i g_{i} gi is the price said by G.
for the i i i -th egg.
If it is impossible to assign the painting, print “-1” (without quotes).
Otherwise print a string, consisting of n n n letters “G” and “A”.
The i i i -th letter of this string should represent the child who will get the i i i -th egg in the required distribution.
Letter “A” represents A.
and letter “G” represents G.
If we denote the money Uncle J. must pay A. for the painting as S a S_{a} Sa , and the money Uncle J. must pay G. for the painting as S g S_{g} Sg , then this inequality must hold: ∣ S a − S g ∣ < = 500 |S_{a}-S_{g}|<=500 ∣Sa−Sg∣<=500 .
If there are several solutions, you are allowed to print any of them.
2
1 999
999 1
AG
3
400 600
400 600
400 600
AGA
#include<bits/stdc++.h> using namespace std; const int N=1e6+10; typedef long long ll; int n,a[N],b[N]; int sa=0,sb=0; string s; int main() { cin>>n; for(int i=1;i<=n;i++){ scanf("%d %d",&a[i],&b[i]); } for(int i=1;i<=n;i++){ if(abs(sa+a[i]-sb)>500){ sb+=b[i]; s+='G'; } else sa+=a[i],s+='A'; } cout<<s<<endl; return 0; }
下面仅仅是题意简述(就是只能不影响做题,想看故事还是需要英文水平的)
有一天,小A和小B很无聊,于是一起玩游戏,游戏是这样的:先给定一个 n ,然后一行给一个只由‘0’和‘1’组成的数列,它们的长度均为 2n ,小A从第一行中选择数,小B从第二行中选择数,小A先选择一个数,假设取的是第 k 个数,那么小B和小A从今往后再也不能选第k个数,当所有人都不能选数时,游戏结束。小A(B)可以任意排列自己拿到的数字,组成一个数,谁的数大谁获胜。
小A获胜则输出“First”
小B获胜则输出“Second”
平局的话输出“Draw”
(输出引号)
输入:第一行一个 n
接下来两行每行一个长度为 2n 的数字
输出:一个字符串(见题意)
Yaroslav, Andrey and Roman can play cubes for hours and hours.
But the game is for three, so when Roman doesn’t show up, Yaroslav and Andrey play another game.
Roman leaves a word for each of them.
Each word consists of 2 ⋅ n 2·n 2⋅n binary characters “0” or “1”.
After that the players start moving in turns.
Yaroslav moves first.
During a move, a player must choose an integer from 1 to 2 ⋅ n 2·n 2⋅n , which hasn’t been chosen by anybody up to that moment.
Then the player takes a piece of paper and writes out the corresponding character from his string.
Let’s represent Yaroslav’s word as s = s 1 s 2 . . . s 2 n s=s_{1}s_{2}...\ s_{2n} s=s1s2... s2n .
Similarly, let’s represent Andrey’s word as t = t 1 t 2 . . . t 2 n t=t_{1}t_{2}...\ t_{2n} t=t1t2... t2n .
Then, if Yaroslav choose number k k k during his move, then he is going to write out character s k s_{k} sk on the piece of paper.
Similarly, if Andrey choose number r r r during his move, then he is going to write out character t r t_{r} tr on the piece of paper.
The game finishes when no player can make a move.
After the game is over, Yaroslav makes some integer from the characters written on his piece of paper (Yaroslav can arrange these characters as he wants).
Andrey does the same.
The resulting numbers can contain leading zeroes.
The person with the largest number wins.
If the numbers are equal, the game ends with a draw.
You are given two strings s s s and t t t .
Determine the outcome of the game provided that Yaroslav and Andrey play optimally well.
The first line contains integer n n n ( 1 < = n < = 1 0 6 1<=n<=10^{6} 1<=n<=106 ).
The second line contains string s s s — Yaroslav’s word.
The third line contains string t t t — Andrey’s word.
It is guaranteed that both words consist of 2 ⋅ n 2·n 2⋅n characters “0” and “1”.
Print “First”, if both players play optimally well and Yaroslav wins.
If Andrey wins, print “Second” and if the game ends with a draw, print “Draw”.
Print the words without the quotes.
2
0111
0001
First
3
110110
001001
First
3
111000
000111
Draw
4
01010110
00101101
First
4
01100000
10010011
Second
#include<bits/stdc++.h> using namespace std; const int N=2e6+10; typedef long long ll; string a,b; int x=0,y=0,z=0,s=0,enda=0,endb=0; int n; int main() { scanf("%n",&n); n*=2; cin>>a>>b; for(int i=1;i<=n;i++){ if(a[i]=='1'&&b[i]=='1') x++; else if(a[i]=='0'&&b[i]=='1') y++; else if(a[i]=='1'&&b[i]=='0') z++; } s=min(y,z); y-=s; z-=s; x%=2; enda+=x; if(enda){ enda+=z/2; endb+=y/2+y%2; } else{ enda+=z/2+z%2; endb+=y/2; } if(enda>endb) printf("First"); else if(enda<endb) printf("Second"); else printf("Draw"); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。