赞
踩
这是昨天的训练赛,在场上A出四题,场下看能不能再补出一题。
Apart from having lots of holidays throughout the year, residents of Berland also have whole lucky years. Year is considered lucky if it has no more than 1 non-zero digit in its number. So years 100, 40000, 5 are lucky and 12, 3001 and 12345 are not.
You are given current year in Berland. Your task is to find how long will residents of Berland wait till the next lucky year.
The first line contains integer number n (1 ≤ n ≤ 109) — current year in Berland.
Output amount of years from the current year to the next lucky one.
4
1
A题:在年份中有且只有一个非零字符的是lucky year。给定今年,问最近的lucky year在几年后。
一开始使用了pow()函数结果WA了很多发,应该是因为pow的返回值类型是double的缘故,后来手写了一个幂函数就过了
- namespace Solver {
- int pp(int x)
- {
- int ans =1;
- for(int i=1; i<=x; i++)
- ans*=10;
- return ans;
- }
-
- void solve(){
- int n;
- cin>>n;
- int p=1;
-
- if(n<10) printf("1\n");
- else {
- while(n/(pp(p))>=10) p++;
- int maxx = (int)(n/(pp(p))+1)*pp(p);
- cout<<maxx - n<<endl;
- }
- }
- };
It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts k days!
When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept records of sleep times for the last n days. So now he has a sequence a1, a2, ..., an, where ai is the sleep time on the i-th day.
The number of records is so large that Polycarp is unable to calculate the average value by himself. Thus he is asking you to help him with the calculations. To get the average Polycarp is going to consider k consecutive days as a week. So there will be n - k + 1 weeks to take into consideration. For example, if k = 2, n = 3 and a = [3, 4, 7], then the result is .
You should write a program which will calculate average sleep times of Polycarp over all weeks.
The first line contains two integer numbers n and k (1 ≤ k ≤ n ≤ 2·105).
The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).
Output average sleeping time over all weeks.
The answer is considered to be correct if its absolute or relative error does not exceed 10 - 6. In particular, it is enough to output real number with at least 6 digits after the decimal point.
3 2 3 4 7
9.0000000000
B题:求给定n个数中,所有连续k长度中所有数的和,求平均值。
用尺取法在O(n)的复杂度内求出所有连续k长度的和。
- void solve(){
- int n, k;
- scanf("%d%d", &n, &k);
- for(int i=0; i<n; i++)
- scanf("%d", &a[i]);
- long long sum = 0, num = 0;
- int l=0, r=k-1;
- for(int i=0; i<k; i++)
- sum += a[i];
- //printf("sum: %d\n", sum);
- num += sum;
- while(r!=n-1)
- {
- sum = sum + a[++r] - a[l++];
- //printf("sum: %d\n", sum);
- num += sum;
- }
-
- double ans = (double)(num)/(double)(n-k+1);
- printf("%.8f", ans);
- }
Polycarp invited all his friends to the tea party to celebrate the holiday. He has n cups, one for each of his n friends, with volumes a1, a2, ..., an. His teapot stores w milliliters of tea (w ≤ a1 + a2 + ... + an). Polycarp wants to pour tea in cups in such a way that:
Friend with cup i won't be satisfied, if there exists such cup j that cup i contains less tea than cup j but ai > aj.
For each cup output how many milliliters of tea should be poured in it. If it's impossible to pour all the tea and satisfy all conditions then output -1.
The first line contains two integer numbers n and w (1 ≤ n ≤ 100, ).
The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 100).
Output how many milliliters of tea every cup should contain. If there are multiple answers, print any of them.
If it's impossible to pour all the tea and satisfy all conditions then output -1.
2 10 8 7
6 4
C题:给n个杯子m单位的水。要求每个杯子中的水是整数,且至少是该杯子容量的一半,容量大的杯子里的水不能比容量小的杯子里的水少,m单位的水要倒完。给出任意一种倒法。
因为题目要求给出任意一种解法,那么直接构造就可以了。首先保证每个杯子里的水都是容量的一半,然后从容量大的杯子开始倒水,把m单位的水倒完。
- struct Node{
- int id;
- int w;
- int h;
- }a[105];
- bool cmp1(Node a, Node b){return a.w>b.w;}
- bool cmp2(Node a, Node b){return a.id<b.id;}
- namespace Solver {
- void solve(){
- int n, m, sum=0;
- scanf("%d%d", &n, &m);
- for(int i=0; i<n; i++){
- scanf("%d", &a[i].w);
- a[i].id = i;
- a[i].h = (a[i].w+1)/2;
- sum += a[i].h;
- }
- if(sum > m) printf("-1\n");
- else {
- sort(a, a+n, cmp1);
- int y = m - sum;
- int p=0;
- while(y>0){
- //printf("y:%d %d\n", p, y);
- if(y > a[p].w - a[p].h){
- y -= a[p].w - a[p].h;
- a[p].h = a[p].w;
- p++;
- }
- else {
- a[p].h += y;
- y=0;
- p++;
- }
- }
- sort(a, a+n, cmp2);
- for(int i=0; i<n; i++){
- printf("%d%c", a[i].h, i==n-1?'\n':' ');
- }
- }
- }
- };
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).
Inserting an element in the same position he was erased from is also considered moving.
Can Vasya divide the array after choosing the right element to move and its new position?
The first line contains single integer n (1 ≤ n ≤ 100000) — the size of the array.
The second line contains n integers a1, a2... an (1 ≤ ai ≤ 109) — the elements of the array.
Print YES if Vasya can divide the array after moving one element. Otherwise print NO.
3 1 3 2
YES
D题:定义一种操作,可以将数列中任意位置的数删去后插入到数列任意位置。问能否通过一次操作使得新的数列中存在前一部分和等于后一部分和的情况。
因为只挪动了一个数,所以可以看成在选取了一部分前缀和后,从后一部分找出一个数挪到前面,或者是从前一部分找出一个数挪到后面。用set来查找。
- multiset<LL > s, d;
- LL a[maxn];
-
-
- namespace Solver {
-
- void solve(){
- int n;
- LL sum=0, num=0;
- s.clear();
- d.clear();
- scanf("%d", &n);
- for(int i=0; i<n; i++){
- scanf("%I64d", &a[i]);
- s.insert(a[i]);
- sum += a[i];
- }
- //printf("sum: %I64d\n", sum);
- if(sum&1) puts("NO");
- else {
- bool can = false;
- sum /= 2;
- for(int i=0; i<n; i++)
- {
- num += a[i];
- multiset <LL>::iterator pos = s.find(a[i]);
- s.erase(pos);
- d.insert(a[i]);
- //printf("%d: %I64d %d\n", i, num, s.count(1));
- if(num == sum){
- can = true;
- break;
- }
- else if(num < sum){
- if(s.count(sum - num)){
- can = true;
- break;
- }
- }
- else if(num > sum){
- if(d.count(num - sum)){
- can = true;
- break;
- }
- }
- }
-
- if(can) puts("YES");
- else puts("NO");
- }
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。