B. Interesting drink
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink “Beecola”, which can be bought in n different shops in the city. It’s known that the price of one bottle in the shop i is equal to xi coins.
Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of “Beecola”.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of shops in the city that sell Vasiliy’s favourite drink.
The second line contains n integers xi (1 ≤ xi ≤ 100 000) — prices of the bottles of the drink in the i-th shop.
The third line contains a single integer q (1 ≤ q ≤ 100 000) — the number of days Vasiliy plans to buy the drink.
Then follow q lines each containing one integer mi (1 ≤ mi ≤ 109) — the number of coins Vasiliy can spent on the i-th day.
Output
Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.
input
5
3 10 8 6 11
4
1
10
3
11
output
0
4
1
5
Note
On the first day, Vasiliy won’t be able to buy a drink in any of the shops.
On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.
On the third day, Vasiliy can buy a drink only in the shop number 1.
Finally, on the last day Vasiliy can buy a drink in any shop.
题目大意
抽象出题目的模型就是给出长度为n的序列,再给出q次询问,每次给出一个数,求序列中比这个数小的数有几个
思路分析:
如果模拟一遍的话肯定要复杂度为O(n*q),肯定要超时的,可以现将序列从小到大排序,然后用二分,找出大于等于前一个数,小于后一个数的序列号,就是我们要求的答案。
在进行二分之前要进行特判,因为如果这个数大于等于所有的数或者这个数小于所有的数都不会对ans进行改变,但是如果大于所有的数的话应该输出的是n,所以先进行特判,然后千万要注意的是在每次询问,也就是for循环之内对ans赋初值,否则的话ans存储的还是上一次询问的答案。
二分内的语句就是先判断符合不符合,符合的话直接跳出循环,不符合的话如果这个数比mid大,就把将l赋值成mid+1,否则将r赋值成mid-1
C. Divisibility by Eight
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn’t contain leading zeroes.
Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn’t have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.
If a solution exists, you should print it.
Input
The single line of the input contains a non-negative integer n. The representation of number n doesn’t contain any leading zeroes and its length doesn’t exceed 100 digits.
Output
Print “NO” (without quotes), if there is no such way to remove some digits from number n.
Otherwise, print “YES” in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.
If there are multiple possible answers, you may print any of them.
Examples
inputCopy
3454
outputCopy
YES
344
inputCopy
10
outputCopy
YES
0
inputCopy
111111
outputCopy
NO
题目大意:
给出一个最长位数为100的数字,然后删除一些数(可以不删),让这个数字变成8的倍数,如果不能变,就输出0,如果能变,就输出变之后的数。
解析:
因为1000可以整除8,所以第四位及其以上的位的数字其实是对这个数是否能整除8是无关紧要的,重要的就是前三位。所以对于一个
C Cinema
Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 109.
In the evening after the conference, all n scientists decided to go to the cinema. There are m movies in the cinema they came to. Each of the movies is characterized by two distinct numbers — the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different).
Scientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists.
Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 200 000) — the number of scientists.
The second line contains n positive integers a1, a2, …, an (1 ≤ ai ≤ 109), where ai is the index of a language, which the i-th scientist knows.
The third line contains a positive integer m (1 ≤ m ≤ 200 000) — the number of movies in the cinema.
The fourth line contains m positive integers b1, b2, …, bm (1 ≤ bj ≤ 109), where bj is the index of the audio language of the j-th movie.
The fifth line contains m positive integers c1, c2, …, cm (1 ≤ cj ≤ 109), where cj is the index of subtitles language of the j-th movie.
It is guaranteed that audio languages and subtitles language are different for each movie, that is bj ≠ cj.
Output
Print the single integer — the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists.
If there are several possible answers print any of them.
Sample Input
Input
3
2 3 2
2
3 2
2 3
Output
2
Input
6
6 3 1 1 3 7
5
1 2 3 4 5
2 3 4 5 1
Output
1
Sample Output
Hint
Source
题意:
有n个外国朋友来中国看电影,他们每个人只会一种语言(不同编号表不同语言示),现在有m部电影,每部电影的声音是a[i],字幕是b[i]。
如果电影的声音他懂的话,他会非常满意,如果字母他能看懂的话他会比较满意,否则它很不满意。
现在问看哪部电影会使得n个外国友人满意最高(如果两部电影使得n个外国友人非常满意的人数相同时,选比较满意的最多的那部电影)。
思路
因为a[i],b[i]很大,所以得用map来存,然后就是计数模拟比较找到最优解就行了。
代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#define N 200010
using namespace std;
map<int,int>s;
int a[N];
int b[N];
struct zz
{
int a;
int b;
}p[N];
int main()在这里插入代码片
{
int n,m,i,j,k;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
s[a[i]]++;
}
scanf("%d",&m);
for(i=1;i<=m;i++)
scanf("%d",&p[i].a);
for(i=1;i<=m;i++)
scanf("%d",&p[i].b);
int mm=0;k=1;
for(i=1;i<=m;i++)
{
if(mm<s[p[i].a])
{
mm=s[p[i].a];
k=i;
}
}
int kk=0;
for(i=1;i<=m;i++)
{
if(s[p[i].a]==mm)
{
if(kk<s[p[i].b])
{
kk=s[p[i].b];
k=i;
}
}
}
printf("%d\n",k);
return 0;
}