点击这里更换您喜欢的皮肤wtboj 首页
请点击这里登入noios   首页 入门 c++讲义 入门教程视频 金牌教程 入门视频 站务 公告 | 题库 记录 竞测 测试 闯关 作业 排名 团队 讨论 | 换肤 | 登入 注册  
News >>   新增功能:各团队管理员可以发布本团队作业了 ()

From sina007
哥德巴赫猜想
讨论 Discussion
 
ssssssssssssssssssssss
#include <iostream>
using namespace std;
int main(){
freopen("p1677.in","r",stdin);
freopen("p1677.out","w",stdout);
int n;
cin >> n;
cout << n << "=";
bool f = true;
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
if (!f) {
cout << "*";
}
cout << i;
f = false;
n /= i;
}
}
if (n > 1) {
if (!f) {
cout << "*";
}
cout << n;
}
fclose(stdin);
fclose(stdout);
return 0;
}
( )

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  // dp[i] 表示以a[i]结尾的最长不下降子序列长度
  vector<int> dp(n, 1);
  // pre[i] 记录以a[i]结尾的最长序列中,前一个元素的下标
  vector<int> pre(n, -1);

  int max_len = 1;  // 最长序列长度
  int last_idx = 0; // 最长序列最后一个元素的下标

  // 动态规划计算dp数组和前驱数组
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (a[j] < a[i] && dp[j] + 1 > dp[i]) {
        dp[i] = dp[j] + 1;
        pre[i] = j;
      }
    }
    // 更新最长序列信息
    if (dp[i] > max_len) {
      max_len = dp[i];
      last_idx = i;
    }
  }

  // 回溯获取最长序列
  vector<int> res;
  while (last_idx != -1) {
    res.push_back(a[last_idx]);
    last_idx = pre[last_idx];
  }
  reverse(res.begin(), res.end()); // 反转得到正确顺序

  // 输出结果
  cout << max_len << endl;
  for (int i = 0; i < res.size(); ++i) {
    if (i > 0) cout << " ";
    cout << res[i];
  }
  cout << endl;

  return 0;
}
( )
发布讨论主题 回复讨论主题
Flag
  
题号
  P1576
  其它
通过
  27人
提交
  85次
通过率
  32%
难度
  1
提交 讨论 题解
 Copyright wtboj © 2005-2006. www.wutuobang.date Powered by wtboj 关于 联系 帮助
 wtboj Information ---- Total Users : 1362 | Online Users / Processes : 0 / 78 | Processed Time : 141 ms | Server Time : 2025/12/16 21:27:19