4.11练习题

第二题:糖糖别胡说,我真的不是签到题目(牛客网)
题意:
在这里插入图片描述
思路主要进行一下预处理就行了,哦,还要从后往前进行判断,主要关注这两个点这道题就差不多了
我的代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
struct surg{
 int  flag;//判断组别
 long long bi;
}shu[50005];
int main(int argc, char** argv) {
 int t,n,m,k;
 int c[50005]={0};
 cin>>t;
 int max0=-1,max1=-1,count=0;
    while(t--){
     cin>>n>>m;
     for(int i=1;i<=n;i++){
      cin>>shu[i].flag;
      cin>>shu[i].bi;
  }
  for(int i=0;i<m;i++){
   cin>>k;
   c[k]++;
  }
  //把所有数据进行发功完成 (预处理)
  for(int i=n;i>0;i--){
   c[i]+=c[i+1];//简单的累加
   shu[i].bi+=c[i]; 
  }
  //从后向前判断(不同组)当前数据后面是否有比当前数据大的
  //如果没有++ 
  for(int i=n;i>0;i--){
   if(!shu[i].flag){
    if(shu[i].bi>max0){
     max0=shu[i].bi;
    }
    if(shu[i].bi>=max1) count++;
   }
   else{
    if(shu[i].bi>max1){
     max1=shu[i].bi;
    }
    if(shu[i].bi>=max0) count++;
   }
  }
  cout<<count<<endl;
  //全部清零 
  max1=-1,max0=-1,count=0;
  memset(c,0,sizeof(c));
  memset(shu,0,sizeof(shu));
 } 
 return 0;
}

第三题 大吉大利,今晚吃鸡(牛客网)
其实就是汉诺塔问题(但是比较细一点)
题意
在这里插入图片描述
我的思路就是一个一个找然后总结出规律
后一个的最小步数时前一个步数*3+2(因为这道题n的范围比较小所以可以这样写n<=25)
我的代码:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
long long f[30];
void dfs(long long x){
 for(int i=2;i<=25;i++){
  f[i]=f[i-1]*3+2;
 }
 return ; 
}
int main(int argc, char** argv) {
 int n;
 f[1]=2;
 //预处理(首先你要找到规律多画几个数据的图) 
 dfs(f[1]);
 while(cin>>n){ 
  printf("%lld\n",f[n]); 
 }
 return 0;
}

参考了别人的代码,大多数人这样写(我感觉不太好理解),这是看自己的,因人而异,可能有人觉得这简单。

#include <iostream>
using namespace std;
typedef long long ll;
ll res = 0;
int n;
void f(char a, char b, char c, ll n){
    if(n == 0){
        return ;
    }
    f(a, b, c, n - 1);
    res++;
    f(c, b, a, n - 1);
    res++;
    f(a, b, c, n - 1);
}
int main(){
    while(scanf("%lld", &n) != EOF){
        res = 0;
        f('a', 'b', 'c', n);
        cout<< res<<endl;
    }
    return 0}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值