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
201
99
4000
1000
In the first example next lucky year is 5. In the second one — 300. In the third — 5000.
题解:将给出数字首位加一,其他数位都为0,做差然后输出就可以了,(例如:201,就变成300,然后减去201,得99)注意,过程中可能爆int,所以用longlong。
代码:
//Wud
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cstring>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 3e5+7;
const int INF = 0x3f3f3f3f;
int m;
int main()
{
while (scanf("%d",&m)!=EOF){
int t;
int cnt = 0;
int n = m;
while (n){
t = n%10;
n/=10;
cnt++;
}
ll ans = t+1;
for(int i = 0;i < cnt-1;i++){
ans*=10;
}
ans = ans-m;
cout<<ans<<endl;
}
return 0;
}
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 sequencea1, 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
1 1 10
10.0000000000
8 2 1 2 4 100000 123 456 789 1
28964.2857142857
In the third example there are n - k + 1 = 7 weeks, so the answer is sums of all weeks divided by 7.
题解:先计算最前面的一段,然后不断向前移动,加上下一个数字,减去上一个数字,得到ans,输出即可。(不可以先预处理出每个数出现次数,然后将所有数字加起来除以(n-k+1),这样会有误差)。
代码:
//Wud
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cstring>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 3e5+7;
const int INF = 0x3f3f3f3f;
int n,k;
int s[maxn];
int main()
{
while (scanf("%d %d",&n,&k)!=EOF){
for(int i = 1;i <= n;i++){
scanf("%d",&s[i]);
}
double ans = 0;
ll sum = 0;
for(int i = 1;i <= k;i++){
sum+=s[i];
}
ans+=sum*1.0/(n-k+1);
for(int i = k+1;i <= n;i++){
sum+=s[i]-s[i-k];
ans+=sum*1.0/(n-k+1);
}
printf("%.10f\n",ans);
}
return 0;
}
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 volumesa1, 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:
- Every cup will contain tea for at least half of its volume
- Every cup will contain integer number of milliliters of tea
- All the tea from the teapot will be poured into cups
- All friends will be satisfied.
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
4 4 1 1 1 1
1 1 1 1
3 10 9 8 10
-1
In the third example you should pour to the first cup at least 5 milliliters, to the second one at least 4, to the third one at least 5. It sums up to 14, which is greater than 10 milliliters available.
题解:先判断是否茶水倒不完(所有杯子容量和小于m),以及不够倒(杯子容量的1/2的和大于m)。然后先让所有杯子的1/2倒满,接着对杯子排序,从高到低把杯子倒满直到茶水完了为止。
代码:
//Wud
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cstring>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 100+7;
const int INF = 0x3f3f3f3f;
int n,k;
struct Node{
int v,id,ans;
} s[maxn];
bool cmp(Node a,Node b){
return a.v < b.v;
}
bool cmp1(Node a,Node b){
return a.id<b.id;
}
int main()
{
while(scanf("%d %d",&n,&k)!=EOF){
for(int i = 1;i <= n;i++){
scanf("%d",&s[i].v);
s[i].id = i;
}
int sum1 = 0,sum2 = 0;
for(int i = 1;i <= n;i++){
sum1+=ceil(s[i].v*1.0/2);
sum2+=s[i].v;
s[i].ans = ceil(s[i].v*1.0/2);
}
if(k>sum2||k < sum1){
cout<<-1<<endl;
continue;
}
sort(s+1,s+n+1,cmp);
k -= sum1;
for(int i = n;i >= 1;i--){
if(k-s[i].v+s[i].ans>0){
k-=s[i].v-s[i].ans;
s[i].ans = s[i].v;
}
else {
s[i].ans = s[i].ans+k;
break;
}
}
sort(s+1,s+n+1,cmp1);
for(int i = 1;i <= n;i++)
{
cout<<s[i].ans<<" ";
}
cout<<endl;
}
return 0;
题解:第一步,先把特判的解决掉,如果原本数列的和就是奇数,那么输出“NO”。第二步,先处理出前缀和,然后枚举每个前段。如果该段的和a大于后段的和b,就可以得到两段差c=a-b,然后在前半段中查找是否存在c/2,如果存在就输出“YES”;如果该段的和a小于后段的b,就在后段中查找c/2,然后同理。如果遍历完所有前缀和都不存在的话,就输出“NO”。而在前段中找c/2的方法是先将整个数列copy一份,然后sort,在新的那一分上二分查找c/2,找到后判断其原来的下标是否大于或者小于现在遍历的i。
代码:
//Wud
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cstring>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
const int INF = 0x3f3f3f3f;
int n;
struct node{
ll v,id;
}s[maxn];
ll u[maxn];
bool cmp(node a,node b){
return a.v<b.v;
}
int lower(int l,int r,ll v){
while(l + 1 < r){
int mid = l+r>>1;
if(s[mid].v>=v){
r = mid;
}
else {
l = mid;
}
}
if(s[l].v==v) return l;
else if(s[r].v==v) return r;
else return -1;
}
int main()
{
while(scanf("%d",&n)!=EOF){
u[0] = 0;
ll sum = 0;
for(int i = 1;i <= n;i++){
scanf("%lld",&u[i]);
s[i].v = u[i];
sum+=u[i];
s[i].id = i;
u[i]+=u[i-1];
}
if(sum%2!=0){
cout<<"NO"<<endl;
continue;
}
sort(s+1,s+1+n,cmp);
bool flag = 0;
for(int i = 1;i <= n;i++){
if(u[i]>sum-u[i]){
ll t = (u[i]-sum+u[i])/2;
int pos = lower(1,n,t);
//cout<<pos<<endl;
if(pos==-1) continue;
for(int j = pos;j <= n&&s[j].v==t;j++){
if(s[j].id<=i) {flag = 1;break;}
}
}
else if(u[i]<sum-u[i]){
ll t = (sum-u[i]-u[i])/2;
int pos = lower(1,n,t);
//cout<<pos<<endl;
if(pos==-1) continue;
for(int j = pos;j <= n&&s[j].v==t;j++){
if(s[j].id>i) {flag = 1;break;}
}
}
else {flag = 1;break;}
if(flag) break;
}
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}