2020 AHUCPC

A

题目描述

给出两个整数 A 和 B,可以重新排列 A 得到新的数字 C (不能有前导0)。求在小于等于 B 的情况下,C 的最大值是多少。如果不存在输出 -1。

复制代码
#include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  int b;
  cin>>s>>b;
  int k=-999;
  sort(s.begin(),s.end());
  do{
    if(s[0]=='0') continue;
    int t=stoll(s);
    if(t<=b) k=max(k,t);
    
  }while(next_permutation(s.begin(),s.end()));
  if(k<0) cout<<-1;
  else cout<<k;
  return 0;
}
复制代码

  

B

 

题目描述

给出 N 个线段,对于线段 i,给出线段在数轴上的左端点 li 和右端点 ri,对于线段 i 和 j,假如 lj < li 并且 ri < rj 就说明线段 i 嵌套在线段 j 中。找到所有嵌套在至少一个其他线段中的线段

贪心:我们将所有左端点排序,优先看右端点小的(可以先计算是否包含,不影响右端点大的结果),模拟

复制代码
#include<bits/stdc++.h>
using namespace std;
const int N =110;
#define PII pair<int,int>
int n;
PII a[N];
bool cmp(PII a,PII b){
    if(a.first!=b.first)return a.first<b.first;
    else return a.second<b.second;
}
int main(){
  cin>>n;
  for(int i=1;i<=n;i++){
    cin>>a[i].first>>a[i].second;
  }
  sort(a+1,a+1+n,cmp);
  int l=-0x3f3f3f3f;
  int r=-0x3f3f3f3f;
  int cnt=0;
//       for(int i=1;i<=n;i++){
//     cout<<a[i].first<<' '<<a[i].second<<endl;
//   }
  for(int i=1;i<=n;i++){
    if(a[i].second>r) {
      r=a[i].second;
      l=a[i].first;  
    }
    else if(a[i].first==l||a[i].second==r){
        continue;
    } 
    else ++cnt;
    
  }
  cout<<cnt;
  return 0;
}
复制代码

 

 

C

 看不懂

F

 

复制代码
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define LL long long
#define ph push_back
#define INF 0x3f3f3f3f
#define PII pair<int, int>
const int N = 100010;
LL a[N];
int n;
LL dp[N][2][2];
int main()
{
  cin >> n;
  for (int i = 1; i < n; i++)
  {
    cin >> a[i];
  }
  for (int i = 2; i <= n; i++)
  {
    dp[i][0][0] = max(dp[i - 1][0][0], dp[i - 1][0][1]) + a[i - 1] - (a[i - 1] % 2 == 0);
    if (a[i - 1] >= 2)
      dp[i][0][1] = dp[i - 1][0][1] + a[i - 1] - (a[i - 1] & 1);
    else
      dp[i][0][1] = 0;
  }
  dp[n][1][0] = 0;
  dp[n][1][1] = 0;
  for (int i = n - 1; i >= 1; i--)
  {
    dp[i][1][0] = max(dp[i + 1][1][0], dp[i + 1][1][1]) + a[i] - (a[i] % 2 == 0);
    if (a[i] >= 2)
      dp[i][1][1] = dp[i + 1][1][1] + a[i] - (a[i] & 1);
    else
      dp[i][1][1] = 0;
  }
  long long res = 0;
  for (int i = 1; i <= n; i++)
  {
    res = max({res, dp[i][0][0], dp[i][1][0]});
    res = max(res, dp[i][0][0] + dp[i][1][0]);
    res = max(res, dp[i][0][0] + dp[i][1][1]);
    res = max(res, dp[i][1][0] + dp[i][0][1]);
    res = max(res, dp[i][0][1] + dp[i][1][1]);
  }
  cout << res;
  return 0;
}
复制代码

 

posted @   蝴蝶眨几次眼睛丶  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示