Codeforces Round #491 (Div. 2)

A. If at first you don't succeed...
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Each student eagerly awaits the day he would pass the exams successfully. Thus, Vasya was ready to celebrate, but, alas, he didn't pass it. However, many of Vasya's fellow students from the same group were more successful and celebrated after the exam.

Some of them celebrated in the BugDonalds restaurant, some of them — in the BeaverKing restaurant, the most successful ones were fast enough to celebrate in both of restaurants. Students which didn't pass the exam didn't celebrate in any of those restaurants and elected to stay home to prepare for their reexamination. However, this quickly bored Vasya and he started checking celebration photos on the Kilogramm. He found out that, in total, BugDonalds was visited by AA students, BeaverKing — by BB students and CC students visited both restaurants. Vasya also knows that there are NN students in his group.

Based on this info, Vasya wants to determine either if his data contradicts itself or, if it doesn't, how many students in his group didn't pass the exam. Can you help him so he won't waste his valuable preparation time?

Input

The first line contains four integers — AA, BB, CC and NN (0A,B,C,N1000≤A,B,C,N≤100).

Output

If a distribution of NN students exists in which AA students visited BugDonalds, BB — BeaverKing, CC — both of the restaurants and at least one student is left home (it is known that Vasya didn't pass the exam and stayed at home), output one integer — amount of students (including Vasya) who did not pass the exam.

If such a distribution does not exist and Vasya made a mistake while determining the numbers AA, BB, CC or NN (as in samples 2 and 3), output 1−1.

Examples
input
Copy
10 10 5 20
output
Copy
5
input
Copy
2 2 0 4
output
Copy
-1
input
Copy
2 2 2 1
output
Copy
-1
Note

The first sample describes following situation: 55 only visited BugDonalds, 55 students only visited BeaverKing, 55 visited both of them and 55students (including Vasya) didn't pass the exam.

In the second sample 22 students only visited BugDonalds and 22 only visited BeaverKing, but that means all 44 students in group passed the exam which contradicts the fact that Vasya didn't pass meaning that this situation is impossible.

The third sample describes a situation where 22 students visited BugDonalds but the group has only 11 which makes it clearly impossible.

A题:暴力模拟就好>>

#include<bits/stdc++.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
using namespace std;
typedef long long ll; 
int main(){
    int a,b,c,n; 
    std::ios::sync_with_stdio(false);cin.tie(0); 
    int flag=1;
    cin>>a>>b>>c>>n;
    if(a>=n||b>=n) flag=0;
    if(a+b-c>=n) flag=0;
    int ans=n-(a+b-c);
    if(ans<0) flag=0;
    if(a<c||b<c) flag=0;
    if(flag==0||ans<=0) cout<<"-1\n";
    else cout<<ans<<endl;
    return 0;
} 
View Code
B. Getting an A
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Translator's note: in Russia's most widespread grading system, there are four grades: 5, 4, 3, 2, the higher the better, roughly corresponding to A, B, C and F respectively in American grading system.

The term is coming to an end and students start thinking about their grades. Today, a professor told his students that the grades for his course would be given out automatically  — he would calculate the simple average (arithmetic mean) of all grades given out for lab works this term and round to the nearest integer. The rounding would be done in favour of the student — 4.54.5 would be rounded up to 55 (as in example 3), but 4.44.4 would be rounded down to 44.

This does not bode well for Vasya who didn't think those lab works would influence anything, so he may receive a grade worse than 55(maybe even the dreaded 22). However, the professor allowed him to redo some of his works of Vasya's choosing to increase his average grade. Vasya wants to redo as as few lab works as possible in order to get 55 for the course. Of course, Vasya will get 55 for the lab works he chooses to redo.

Help Vasya — calculate the minimum amount of lab works Vasya has to redo.

Input

The first line contains a single integer nn — the number of Vasya's grades (1n1001≤n≤100).

The second line contains nn integers from 22 to 55 — Vasya's grades for his lab works.

Output

Output a single integer — the minimum amount of lab works that Vasya has to redo. It can be shown that Vasya can always redo enough lab works to get a 55.

Examples
input
Copy
3
4 4 4
output
Copy
2
input
Copy
4
5 4 5 5
output
Copy
0
input
Copy
4
5 3 3 5
output
Copy
1
Note

In the first sample, it is enough to redo two lab works to make two 44s into 55s.

In the second sample, Vasya's average is already 4.754.75 so he doesn't have to redo anything to get a 55.

In the second sample Vasya has to redo one lab work to get rid of one of the 33s, that will make the average exactly 4.54.5 so the final grade would be 55.

B题:multiset+优先将最小的变成5+模拟

#include<bits/stdc++.h>
using namespace std;
multiset<double>aa;
int n;
bool eps(double a,double b){
    if(fabs(a-b)<=0.5) return true;
    else return false;
}
double ave(){
    double res=0;
    for(auto it=aa.begin();it!=aa.end();it++){
        res+=*it;
    }
    res/=n;
    return res;
}
int main(){
    std::ios::sync_with_stdio(false);
    double x;cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x;aa.insert(x);
    }
    int ans=0;
    while(!eps(5.0,ave())){
        auto it=aa.begin();
        aa.erase(it);
        aa.insert(5);
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}
View Code
C. Candies
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

After passing a test, Vasya got himself a box of nn candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself.

This means the process of eating candies is the following: in the beginning Vasya chooses a single integer kk, same for all days. After that, in the morning he eats kk candies from the box (if there are less than kk candies in the box, he eats them all), then in the evening Petya eats 10%10% of the candies remaining in the box. If there are still candies left in the box, the process repeats — next day Vasya eats kk candies again, and Petya — 10%10% of the candies left in a box, and so on.

If the amount of candies in the box is not divisible by 1010, Petya rounds the amount he takes from the box down. For example, if there were 9797 candies in the box, Petya would eat only 99 of them. In particular, if there are less than 1010 candies in a box, Petya won't eat any at all.

Your task is to find out the minimal amount of kk that can be chosen by Vasya so that he would eat at least half of the nn candies he initially got. Note that the number kk must be integer.

Input

The first line contains a single integer nn (1n10181≤n≤1018) — the initial amount of candies in the box.

Output

Output a single integer — the minimal amount of kk that would allow Vasya to eat at least half of candies he got.

Example
input
Copy
68
output
Copy
3
Note

In the sample, the amount of candies, with k=3k=3, would change in the following way (Vasya eats first):

686559565148444137343128262321181714131096633068→65→59→56→51→48→44→41→37→34→31→28→26→23→21→18→17→14→13→10→9→6→6→3→3→0.

In total, Vasya would eat 3939 candies, while Petya — 2929.

C题: 二分答案>>考虑k越大>>每次被另一个人拿走的就越少>>因为总数是一定的了>>所以该题满足单调性>>二分答案即可>>

另外的一点小收获>>即sum/=10>>向下取整>>>

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
ll ok(ll x){
    if(!x) return 0;
    ll sum=n,ans=0;
    while(sum){
        if(sum>=x) ans+=x,sum-=x;
        else ans+=sum,sum=0;
        sum-=sum/10;
    }
    return ans;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n;ll l=0,r=n;ll cmp=(n/2)+n%2;
    while(l<r){
        ll mid=(l+r)>>1;
        if(ok(mid)<cmp) l=mid+1;
        else r=mid;
    }
    cout<<r<<endl;
    return 0;
}
View Code
D. Bishwock
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:


XX XX .X X.
X. .X XX XX

Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.

Vasya has a board with 2×n2×n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.

Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns.

Input

The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100100.

Output

Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.

Examples
input
Copy
00
00
output
Copy
1
input
Copy
00X00X0XXX0
0XXX0X00X00
output
Copy
4
input
Copy
0X0X0
0X0X0
output
Copy
0
input
Copy
0XXX0
00000
output
Copy
2

D题:位置不合适>>看题意必须有一个位置上下全为空时才能放>>每次暴力查询位置如果前面不能放>>则放后面>>先前后后>>
保证无后效性>>

#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int main(){
    cin>>s1>>s2;int n=s1.size();
    int ans=0;
    for(int i=0;i<n;i++){
        if(s1[i]=='0'&&s2[i]=='0'){
            int tmp=0;
            if(i-1>=0){
                if(s1[i-1]=='0'||s2[i-1]=='0'){
                    s1[i]='X',s2[i]='X';int flag=0;
                    if(s1[i-1]=='0') s1[i-1]='X',flag=1;
                    if(s2[i-1]=='0'&&flag==0) s2[i-1]='X';
                    ans++;tmp=1;
                }
            }
            if(tmp==0){
                if(i+1<n){
                    if(s1[i+1]=='0'||s2[i+1]=='0'){
                        s1[i]='X',s2[i]='X';int flag=0;
                        if(s1[i+1]=='0') s1[i+1]='X',flag=1;
                        if(s2[i+1]=='0'&&flag==0) s2[i+1]='X';
                        ans++;tmp=1;
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
View Code
E. Bus Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

This night wasn't easy on Vasya. His favorite team lost, and he didn't find himself victorious either — although he played perfectly, his teammates let him down every time. He had to win at least one more time, but the losestreak only grew longer and longer... It's no wonder he didn't get any sleep this night at all.

In the morning, Vasya was waiting the bus to the university on the bus stop. Vasya's thoughts were hazy and so he couldn't remember the right bus' number quite right and got onto the bus with the number nn.

In the bus, Vasya thought that he could get the order of the digits in the number of the bus wrong. Futhermore, he could "see" some digits several times, but the digits he saw were definitely in the real number of the bus. For example, if Vasya saw the number 2028, it could mean that the real bus number could be 2028, 8022, 2820 or just 820. However, numbers 80, 22208, 52 definitely couldn't be the number of the bus. Also, real bus number couldn't start with the digit 0, this meaning that, for example, number 082 couldn't be the real bus number too.

Given nn, determine the total number of possible bus number variants.

Input

The first line contains one integer nn (1n10181≤n≤1018) — the number of the bus that was seen by Vasya. It is guaranteed that this number does not start with 00.

Output

Output a single integer — the amount of possible variants of the real bus number.

Examples
input
Copy
97
output
Copy
2
input
Copy
2028
output
Copy
13
Note

In the first sample, only variants 9797 and 7979 are possible.

In the second sample, the variants (in the increasing order) are the following: 208208, 280280, 802802, 820820, 20282028, 20822082, 22082208, 22802280, 28022802, 28202820, 80228022, 82028202, 82208220.

E题:dfs+组合数>>让我对暴力的用法有了新的认识
首先找到能暴力的地方>>我们考虑枚举每个数需要用的个数>>确定好1-9之后我们在确定1放的位置
同时用了组合数方案去重的公式
p=a1+a2+..+a9;
k=p!/a1!/..../a1!;
就是将b个0放在a个数后>>即用隔板法将0给划分为 a分 C(a-1,a+b-1);>>即可

#include<bits/stdc++.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
using namespace std;
typedef long long ll;
vector<ll>d;ll n,res,p[20],f[10];
ll C(ll n,ll m){return p[n]/p[m]/p[n-m];}
void dfs(ll x)
{
    if(x==10)
    {
        ll k=1,s=0;
        for(auto t:d)s+=t;k=p[s];
        for(auto t:d)k/=p[t];
        if(!f[0]){res+=k;return;}
        for(ll i=1;i<=f[0];i++)res+=k*C(i+s-1,s-1);
        return;
    }
    if(!f[x])dfs(x+1);
    for(ll i=1;i<=f[x];i++) d.push_back(i),dfs(x+1),d.pop_back();
}
void init(){p[0]=1;for(ll i=1;i<20;i++)p[i]=p[i-1]*i;}
void solve(){
    cin>>n;while(n)f[n%10]++,n/=10;
    dfs(1);cout<<res<<endl;
}
int main()
{
    init();solve();
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/vainglory/p/9224970.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值