习题日常第二练

1花生采摘(题目链接

这个题确实有不用递归的搜索,但是作为一个对于搜索不太熟练的新手,用递归搜索写了这个题还是对于搜索有了一定掌握。这个题看似复杂,实际比较简单,对于每一个有花生的地方用结构体存下,排序,然后逐个搜索,采用深度优先搜索的办法,直到搜不动或者搜完为止,如果所需要时间已经大于所给时间,回溯,具体见代码。搜索的时候一定要先判它是不是最后一个(不然只有一个格子有花生的情况会wa)。代码如下。

#include<cmath>
#include <iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<cstring>
#include<math.h>
#include<stack>
#include<algorithm>
#include<queue>
#include<bitset>
#define ll long long int
const int mod=1e9+7;
using namespace std;
char b[30010];
struct node
{
    int x,y,lp;
}t[40010];
int ans,id=0;
int dfs(int ri)
{
      if(ri==id-1)
    {
         ans-=(abs(t[ri-1].x-t[ri].x)+abs(t[ri-1].y-t[ri].y));
          ans--;
          if(ans>=t[ri].x)
            return t[ri].lp;
          else
          {
            ans+=(abs(t[ri-1].x-t[ri].x)+abs(t[ri-1].y-t[ri].y));
            ans++;
            return 0;
          }
    }
    else if(ri==0)
    {
        ans-=t[ri].x;
        int p=dfs(ri+1);
        if(!p)
        {
            if(ans>=t[ri].x)
            {
                return t[ri].lp;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return t[ri].lp+p;
        }
    }

    else
    {
        if(ans<=0)
            return 0;
        else
        {
          ans-=(abs(t[ri-1].x-t[ri].x)+abs(t[ri-1].y-t[ri].y));
          ans--;
          int p=dfs(ri+1);
          if(p)
          {
              return p+t[ri].lp;
          }
          else
          {
              if(ans>=t[ri].x)
              {
                  return t[ri].lp;
              }
              else
              {
                  ans+=(abs(t[ri-1].x-t[ri].x)+abs(t[ri-1].y-t[ri].y));
                  ans++;
                  return 0;
              }
          }
        }
    }
}
bool cmp(node p,node q)
{
    return p.lp>q.lp;
}
int main()
{
    ios::sync_with_stdio(false);
    ll n,k,i,j,m=0,sum=0,pi;
  cin>>n>>m>>ans;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            cin>>k;
            if(k!=0)
            {
                t[id].x=i;
                t[id].y=j;
                t[id].lp=k;
                id++;
            }
        }
    }
   sort(t,t+id,cmp);
   ans--;
   k=dfs(0);
   cout<<k;
  return 0;
}

2进制转换(题目链接

这个题是个数学题,不难但是要对于进制转换要有一定的理解,尤其是平常用到的取模运算是正数,负数的进制转换大同小异。对于进制的转换,我们平常使用的是余数逆序输出的办法。对于任意被除数=除数*商+余数=(商+1)*除数+(余数-除数),余数-除数作为新的余数必定为正数。代码如下。

#include<cmath>
#include <iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<cstring>
#include<math.h>
#include<stack>
#include<algorithm>
#include<queue>
#include<bitset>
#define ll long long int
const int mod=1e9+7;
using namespace std;
char b[30010];
void dfs(int n,int r)
{
    if(n==0)
    {
       return;
    }
    int m=n%r;
    if(m<0)
    {
        m-=r;
        n+=r;
    }
    char p;
    if(m>=10)
        p=m+'A'-10;
    else
        p=m+'0';
    dfs(n/r,r);
    cout<<p;
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    ll n,k,i,j,m=0,sum=0,pi;
  cin>>n>>m;
  cout<<n<<"=";
  dfs(n,m);
  cout<<"(base"<<m<<")";
  return 0;
}

3导弹拦截(题目链接

这个题是一个求最长不上升子序列和最长上升子序列的题,一开始以为是动态规划,但是数据太大,看过别人的题解以后发现求子序列问题可以有n*log(n)的算法。利用stl里面的lower__bound()和upper__bound()来维护,每一个数维护一次,维护一次所需log(n)。这种做法是需要新开一个数组,拿最长不上升序列为例,每一次判断这个数字是否小于等于新数组的最后一个数,如果小于等于则添加,如果大于,找新数组中第一个小于等于它的数,将其替换。具体操作见如下代码。(lower__bound()和upper__bound()一般用在递增序列,如果另外有疑问推荐看链接

#include<cmath>
#include <iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<cstring>
#include<math.h>
#include<stack>
#include<algorithm>
#include<queue>
#include<bitset>
#define ll long long int
const int mod=1e9+7;
using namespace std;
int a[100010],b[100010],c[100010];
int main()
{
    ios::sync_with_stdio(false);
    ll n,k,i=0,j,m=0,sum=0,pi;
    while(cin>>a[i++]);
    i--;
        n=i;
        int len1=0,len2=0;
        b[0]=a[0];
        c[0]=b[0];
        for(i=1;i<n;i++)
        {
            if(a[i]<=b[len1])
            {
                b[++len1]=a[i];
            }
            else
            {
                j=upper_bound(b,b+len1+1,a[i],greater<int>())-b;
                b[j]=a[i];
            }
        }
        for(i=1;i<n;i++)
        {
            if(a[i]>c[len2])
            {
                c[++len2]=a[i];
            }
            else
            {
                j=lower_bound(c,c+len2+1,a[i])-c;
                c[j]=a[i];
            }
        }
       cout<<len1+1<<endl<<len2+1;
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值