581C
2015-10-02 17:33
519 查看
题目链接:点击打开链接 (这道题除了最后的代码,我都套用的官方的解释)
C. Developing Skills
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya loves computer games. Finally a game that he's been waiting for so long came out!
The main character of this game has n different skills, each of which is characterized by an integer ai from
0 to 100. The higher the number ai is,
the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values of
for
all i from 1 to n.
The expression ⌊ x⌋ denotes the result of rounding the number x down to
the nearest integer.
At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his
total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46,
after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.
Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
Input
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107)
— the number of skills of the character and the number of units of improvements at Petya's disposal.
The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100),
where ai characterizes
the level of the i-th skill of the character.
Output
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or
less improvement units.
Sample test(s)
input
output
input
output
input
output
Note
In the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating
of the character becomes equal to lfloor frac{100}{10} rfloor + lfloor frac{100}{10} rfloor = 10 + 10 = 20.
In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he
will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is
.
In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So
the answer is equal to
.
题目分析:
This problem can be solved in many ways. Let's consider the most intuitive way that fits in the given time.
In the beginning we need to sort given array in the following way — from two numbers to the left should be the number to which must be added fewer units of improvements to make it a multiple of 10. You must add at least one unit of energy to every of this numbers.
For example, if given array is {45, 30, 87, 26} after the sort the array must be equal to {87, 26, 45, 30}.
Now we iterate on the sorted array for i from 1 to n.
Let's cur = 10 - (aimod10).
If cur ≤ k assign ai = ai + cur and
from k subtract curelse
if cur > k break from cycle.
The next step is to iterate on array in the same way.
Now we need only to calculate answer ans — we iterate on array for i from
1 to n and assign ans = ans + (ai / 10).
Asymptotic behavior of this solution — O(n * log(n)) where n is
the number of hero skills.
C. Developing Skills
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya loves computer games. Finally a game that he's been waiting for so long came out!
The main character of this game has n different skills, each of which is characterized by an integer ai from
0 to 100. The higher the number ai is,
the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values of
for
all i from 1 to n.
The expression ⌊ x⌋ denotes the result of rounding the number x down to
the nearest integer.
At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his
total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46,
after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.
Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
Input
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107)
— the number of skills of the character and the number of units of improvements at Petya's disposal.
The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100),
where ai characterizes
the level of the i-th skill of the character.
Output
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or
less improvement units.
Sample test(s)
input
2 4 7 9
output
2
input
3 8 17 15 19
output
5
input
2 299 100
output
20
Note
In the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating
of the character becomes equal to lfloor frac{100}{10} rfloor + lfloor frac{100}{10} rfloor = 10 + 10 = 20.
In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he
will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is
.
In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So
the answer is equal to
.
题目分析:
This problem can be solved in many ways. Let's consider the most intuitive way that fits in the given time.
In the beginning we need to sort given array in the following way — from two numbers to the left should be the number to which must be added fewer units of improvements to make it a multiple of 10. You must add at least one unit of energy to every of this numbers.
For example, if given array is {45, 30, 87, 26} after the sort the array must be equal to {87, 26, 45, 30}.
Now we iterate on the sorted array for i from 1 to n.
Let's cur = 10 - (aimod10).
If cur ≤ k assign ai = ai + cur and
from k subtract curelse
if cur > k break from cycle.
The next step is to iterate on array in the same way.
Now we need only to calculate answer ans — we iterate on array for i from
1 to n and assign ans = ans + (ai / 10).
Asymptotic behavior of this solution — O(n * log(n)) where n is
the number of hero skills.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n, k; int a[100000+10]; bool cmp(const int &a, const int &b){ return a%10 > b%10; } int main() { while(scanf("%d%d", &n, &k) != EOF){ for(int i = 0; i < n; ++i) scanf("%d", &a[i]); sort(a, a+n, cmp); for(int i = 0; i < n; ++i){ int cur = 10-a[i]%10; if(k >= cur && a[i] != 100) {k-=cur; a[i]+=cur;} } sort(a, a+n); for(int i = 0; i < n; ++i){ while(a[i] != 100 && k >= 10) {a[i]+=10; k-=10;} } int o = 0; for(int i = 0; i < n; ++i) o += a[i]/10; printf("%d\n", o); } return 0; }<span style="font-size: 14px;"> </span>
相关文章推荐
- 基础算法 KMP
- typedef
- 使用堆栈将缺少左括号的表达式补全并计算其值
- light oj 1384 - Stream My Contest (最小树形图+二分)
- apache 占用内存总量与每个apache进程的平均内存占用量计算
- Android Material Design的FloatingActionButton,Snackbar和CoordinatorLayout
- hihoCoder 1234 Fractal
- 从源码的角度详解View,ViewGroup的Touch事件的分发机制
- 计算器
- ASCII、Unicode、GBK和UTF-8字符编码的区别联系
- 2015 四川省赛 I Travel(bfs)
- hdu 3790 双权值最短路径
- 软工之测试与维护
- 弱校联萌十一大决战之强力热身 A. Easy Math (水)
- 【分享】如何尊重一个程序员?
- Peacock:大规模主题模型及其在腾讯业务中的应用
- js 验证表单 js提交验证类
- Android数据存储通用策略
- Sudoku Solver
- 简单导航页制作--代码