codeforces-915C Permute Digits

C. Permute Digits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.

It is allowed to leave a as it is.

Input

The first line contains integer a (1 ≤ a ≤ 1018). The second line contains integer b (1 ≤ b ≤ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists.

Output

Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.

The number in the output should have exactly the same length as number a. It should be a permutation of digits of a

题目大意:给定两个正整数A,B(1<=A,B<=10e18),现在你可以对A进行任意排序,问在排序后A不大于B的情况下,A的最大值是多少。题目保证A、B没有前缀零。

题目分析:我是用贪心+DFS+剪枝优化过的,关键的地方在于剪枝,在排序后选取小于或等于B[i]时都是一次性的,具体的可以看看代码。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+100;
int N,M,pos,l,r,judge=0;
string ax,bx,cx,dx;
int vis[100];
void dfs(int now,int flag)
{
   if(flag)
    {
           for(int i=0;i<ax.length();i++)
           {
               if(!vis[i])
                dx+=ax[i];
           }
           cx=dx;
           judge=1;
           return ;
    }
   if(now==bx.length())
   {
       cx = dx;
       judge=1;
       return ;
   }
   int temp =0;
   for(int i=0;i<ax.length();i++)
   {
       if(!vis[i]&&ax[i]<bx[now])
       {
           dx+=ax[i];
            vis[i]=1;
           dfs(now+1,flag|1);
           return ;
       }
       if(!vis[i]&&ax[i]==bx[now]&&!temp)
       {
           dx+=ax[i];
           vis[i]=1;
           temp = 1 ;
           dfs(now+1,flag);
           if(judge)
            return ;
           else
           {
                vis[i]=0;
                dx = dx.substr(0,dx.length() - 1);
           }
       }
       if(judge)
        return ;
   }
}
int main()
{

     std::ios::sync_with_stdio(false);
     std::cin.tie(0);
     while(cin>>ax>>bx)
     {
         judge=0;
         dx = "";
         memset(vis,0,sizeof(vis));

         if(ax.length()<bx.length())
         {
             sort(ax.begin(),ax.end());
             reverse(ax.begin(),ax.end());
             cout<<ax<<endl;
             continue;
         }
         while(bx.length()<ax.length())
            bx = '0'+bx;
         vector<char> ac;
         vector<char> ans;
        sort(ax.begin(),ax.end());
        reverse(ax.begin(),ax.end());
            dfs(0,0);
           cout<<cx<<endl;
     }
    return 0;
}
   其实我觉得我这个方法蛮笨的,后面去看了别人的代码,感觉到了好大的差距= =..

   首先是string其实是很好用的,它自带很多很好的操作,很方便,像上面的          substring(begin,end)提取子字符串就很舒服。然后下面会用到stoll,是字符串转为long long类型,当然还有stoi转int。还会用到prev_permutation,和next_permutation相对,这个是求上一个排列的字符串。

下面的代码是学习别人的,主要就是从前往后,保证当前比较位最大,这样最后总串也就最大了,很巧妙。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
   string a;
   LL b;
   cin>>a>>b;
   priority_queue<int,vector<int>,greater<int> >Q;
   for(int i=0;i<a.length();i++)
   {
       sort(a.begin()+i,a.end(),greater<char>());
       sort(a.begin()+i+1,a.end());
       while(stoll(a)>b)
       {
           prev_permutation(a.begin()+i,a.end());
           sort(a.begin()+i+1,a.end());
       }
   }
   cout<<a<<endl;

   return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值