A.
简单的容斥原理,注意一些奇怪的大小问题即可。
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
int a,b,c,n;
cin>>a>>b>>c>>n;
if(c>a||c>b||c>a+b||a+b-c+1>n){
cout<<-1<<endl;return 0;
}
cout<<n-(a+b-c)<<endl;
return 0;
}
B.
贪心搞搞就可以了。
#include<cstdio>
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
int n,i,j,k;
double tar,ans=0;
cin>>n;
multiset<int>s;
for(i=1;i<=n;i++){
cin>>j;s.insert(j);ans+=j;
}
tar=n*4.5;
int cnt=0;
while(ans<tar){
ans+=(5-(*s.begin()));s.erase(s.begin());cnt++;
}
cout<<cnt<<endl;
return 0;
}
C.
注意到k越大时他能吃到的糖一定是越多 的,因此我们可以在此基础上对k进行二分,至于判断他能在这个情况下吃糖的数目,根据题意模拟即可。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
long long n,x,y,z,L=0,R,k;
cin >> n;
R = n;
while (R - L > 1) {
k = (R + L) / 2;
x = n;y = 0;
while (x) {
z = min(x, k);
y += z;x -= z;
x -= x / 10;
}
if (2 * y >= n) R = k;
else L = k;
}
cout << R << endl;
return 0;
}
D.
明显可以动态规划来做,但是有些细节需要小心。首先,用f[i][j]表示到第i列时末尾这列放置的是第j种block时的最大可能数,那么f[i][j]可以从f[i-2]k以及f[i-1]k转移而来。同时需要注意初始化的问题,这一列的每种情况至少是上一列的所有情况的最大值(因为统计的是最大的和);
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int pos[3][105];
bool judge(int x,int type)//判断状态是否可放
{
if(type==1){
if(pos[1][x-1]==0&&pos[2][x-1]==0&&pos[1][x]==0)
return true;
else return false;
}
else if(type==2){
if(pos[1][x-1]==0&&pos[2][x]==0&&pos[1][x]==0)
return true;
return false;
}
else if(type==3){
if(pos[2][x-1]==0&&pos[1][x]==0&&pos[2][x]==0)
return true;
return false;
}
else if(type==4){
if(pos[1][x-1]==0&&pos[2][x-1]==0&&pos[2][x]==0)
return true;
return false;
}
return false;
}
int f[105][5];
int main()
{
string str;
int i,j,k;
for(i=1;i<=2;i++){
cin>>str;
for(j=0;j<str.size();j++){
if(str[j]=='X')
pos[i][j+1]=1;
}
}
int n=str.size();
for(i=2;i<=n;i++){
for(j=1;j<=4;j++){
if(judge(i,j)){
for(k=1;k<=4;k++){
f[i][j]=max(f[i][j],f[i-2][k]+1);
}
if(j==2){
f[i][j]=max(f[i][j],f[i-1][4]+1);
}
else if(j==3){
f[i][j]=max(f[i][j],f[i-1][1]+1);
}
}
for(k=1;k<=4;k++){
f[i][j]=max(f[i][j],f[i-1][k]);
}
}
}
int ans=0;
for(i=1;i<=4;i++)
ans=max(ans,f[n][i]);
cout<<ans<<endl;
return 0;
}
E.
题目并不太好理解,但是其实说起来也并不太复杂。意思就是读入一个数,原数中的所有数字构成的集合一定是这个数的子集,同时这个数中的所有数一定在原数中出现过,问原数有多少种可能。注意,原数的开头不能是0.
一种偶素暴力的想法就是统计这个数中的每个数的出现次数,然后写一个10重的循环,枚举0~9每个数出现的次数并计算在此情况下的可能的原数(因为不同排列会构成不同的原数)。想法是没问题的,但是会tle(摔
代码:
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
int cnt[10];
typedef long long ll;
int main()
{
//freopen("1.in","r",stdin);
//freopen("bl.out","w",stdout);
ll num;int i,j,k,a,b,c,d,e,f,g,h,l,m,n,o,p;
cin>>num;
while(num){
p=num%10;num/=10;cnt[p]++;
}
int ans=0;
for(a=cnt[0]?1:0;a<=cnt[0];a++){
vector<int>pos1;
if(a)pos1.push_back(0);
for(b=cnt[1]?1:0;b<=cnt[1];b++){
vector<int>pos2=pos1;
if(b){
for(p=1;p<=b;p++)
pos2.push_back(1);
}
for(c=cnt[2]?1:0;c<=cnt[2];c++){
vector<int>pos3=pos2;
if(c){
for(p=1;p<=c;p++)
pos3.push_back(2);
}
for(d=cnt[3]?1:0;d<=cnt[3];d++){
vector<int>pos4=pos3;
if(d){
for(p=1;p<=d;p++)
pos4.push_back(3);
}
for(e=cnt[4]?1:0;e<=cnt[4];e++){
vector<int>pos5=pos4;
if(e){
for(p=1;p<=e;p++)
pos5.push_back(4);
}
for(f=cnt[5]?1:0;f<=cnt[5];f++){
vector<int>pos6=pos5;
if(f){
for(p=1;p<=f;p++)
pos6.push_back(5);
}
for(g=cnt[6]?1:0;g<=cnt[6];g++){
vector<int>pos7=pos6;
if(g){
for(p=1;p<=g;p++)
pos7.push_back(6);
}
for(h=cnt[7]?1:0;h<=cnt[7];h++){
vector<int>pos8=pos7;
if(h){
for(p=1;p<=h;p++)
pos8.push_back(7);
}
for(l=cnt[8]?1:0;l<=cnt[8];l++){
vector<int>pos9=pos8;
if(l){
for(p=1;p<=l;p++)
pos9.push_back(8);
}
for(m=cnt[9]?1:0;m<=cnt[9];m++){
vector<int>pos10=pos9;
if(m){
for(p=1;p<=m;p++)
pos10.push_back(9);
}
sort(pos10.begin(),pos10.end());
for(p=0;p<pos10.size();p++)
if(pos10[p])break;
swap(pos10[0],pos10[p]);ans++;
//for(auto a:pos10)cout<<a;
//cout<<endl;
while(next_permutation(pos10.begin(),pos10.end())){
ans++;
//for(auto a:pos10)cout<<a;
//cout<<endl;
}
}
}
}
}
}
}
}
}
}
}
//cout<<cnt[8]<<endl;
cout<<ans<<endl;
return 0;
}
结果:
说说正解吧…
同样的,我们统计这个数中每一个数字出现的次数,然后对于每一位,我们枚举他出现的次数。与上面不同的是,我们并不暴力next_permutation,而是利用排列组合求解。而组合数是可以预处理出来的,所以复杂度下降了很多。并且,我们只枚举0~9中每个数出现的次数,并不像上面那样真的把每个数加入到集合里面去排列。
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll p[20];
ll res=0,cnt[10];
vector<int>pos;
ll c(ll x,ll y)//计算组合数,c(x,y)=x!/(x-y)!/y!
{
return p[x]/p[x-y]/p[y];
}
void dfs(int x)
{
if(x==10){//如果已经枚举到了9,就可以开始计算在这种情况下的可能的原数的数量
int s=0;ll t;//s统计这种情况下原数的位数,t是s排列的可能数
for(auto a:pos)s+=a;
t=p[s];
for(auto a:pos)t/=p[a];//因为每种数很可能会重复,因此对于每一个数要除以他的排列数
if(!cnt[0]){//如果见到的数中没有0,这就是答案
res+=t;return;
}
for(int i=1;i<=cnt[0];i++){//否则,枚举原数中0的出现次数,对于每一个可能,
// 原数中第一位一定不能是0,后面的i+s-1位中选s-1位给非0数即可
res+=t*c(i+s-1,s-1);
}
return;
}
if(!cnt[x])dfs(x+1);//如果没有这个数,直接跳过
for(int i=1;i<=cnt[x];i++){//枚举这个数出现各种个数的可能
pos.push_back(i);dfs(x+1);pos.pop_back();
}
return;
}
int main()
{
//freopen("1.in","r",stdin);
//freopen("zj.out","w",stdout);
int i,j,k;ll num;
cin>>num;
p[0]=1;
for(i=1;i<20;i++)
p[i]=p[i-1]*i;//预处理阶乘
while(num){
cnt[num%10]++;num/=10;//统计各种数出现次数
}
dfs(1);
cout<<res<<endl;
return 0;
}