mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-12-22 15:11:48 +00:00
Compare commits
No commits in common. "84eee83148c1abd65dfff0e67636e6dfd7801343" and "8b49a88aa909fd39807f79ca99d24bade82ca075" have entirely different histories.
84eee83148
...
8b49a88aa9
@ -1,16 +1,10 @@
|
|||||||
# 算法笔记
|
|
||||||
## 线性动态规划优化为$O(n\log{n})$方法
|
## 线性动态规划优化为$O(n\log{n})$方法
|
||||||
>如果是递增序列就lower_bound
|
>如果是递增序列就lower_bound
|
||||||
|
|
||||||
>如果是递减序列就手写二分
|
>如果是递减序列就手写二分
|
||||||
|
|
||||||
## 区间dp
|
## 区间dp
|
||||||
### 步骤
|
|
||||||
1. 根据问题推出dp含义
|
1. 根据问题推出dp含义
|
||||||
2. 根据规则写出dp的状态转移公式
|
2. 根据规则写出dp的状态转移公式
|
||||||
3. 处理边界问题
|
3. 处理边界问题
|
||||||
> dp[i][j], dp[0][0], dp[i][0], dp[0][j], dp[i][i], dp[j][j]
|
> dp[i][j], dp[0][0], dp[i][0], dp[0][j], dp[i][i], dp[j][j]
|
||||||
###
|
|
||||||
1. 编辑距离 i-1,j i,j-1
|
|
||||||
2. 合并石子 1~k,k+1~i
|
|
||||||
3. 网捉蛇 1~k用j-1, k+1~i用1
|
|
||||||
@ -1,94 +0,0 @@
|
|||||||
#include <algorithm>
|
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class Solution {
|
|
||||||
public:
|
|
||||||
const vector<vector<int>> dir = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
|
|
||||||
int n, m;
|
|
||||||
|
|
||||||
bool isValid(int r, int c) {
|
|
||||||
return r >= 0 && r < n && c >= 0 && c < m;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int getClockwiseTurn(int dir_idx) {
|
|
||||||
return (dir_idx + 1) % 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getExpectedValue(int len) {
|
|
||||||
return (len % 2 == 0) ? 2 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int dfs_second_leg(const vector<vector<int>>& g, int r, int c, int len, int dir_idx) {
|
|
||||||
int max_len_from_here = len;
|
|
||||||
|
|
||||||
int next_r = r + dir[dir_idx][0];
|
|
||||||
int next_c = c + dir[dir_idx][1];
|
|
||||||
|
|
||||||
if (isValid(next_r, next_c) && g[next_r][next_c] == getExpectedValue(len + 1)) {
|
|
||||||
max_len_from_here = max(max_len_from_here, dfs_second_leg(g, next_r, next_c, len + 1, dir_idx));
|
|
||||||
}
|
|
||||||
|
|
||||||
return max_len_from_here;
|
|
||||||
}
|
|
||||||
|
|
||||||
int dfs_first_leg(const vector<vector<int>>& g, int r, int c, int len, int dir_idx) {
|
|
||||||
int max_len_found = len;
|
|
||||||
|
|
||||||
int turn_dir_idx = getClockwiseTurn(dir_idx);
|
|
||||||
int next_r_turn = r + dir[turn_dir_idx][0];
|
|
||||||
int next_c_turn = c + dir[turn_dir_idx][1];
|
|
||||||
|
|
||||||
if (isValid(next_r_turn, next_c_turn) && g[next_r_turn][next_c_turn] == getExpectedValue(len + 1)) {
|
|
||||||
|
|
||||||
max_len_found = max(max_len_found, dfs_second_leg(g, next_r_turn, next_c_turn, len + 1, turn_dir_idx));
|
|
||||||
}
|
|
||||||
|
|
||||||
int next_r_straight = r + dir[dir_idx][0];
|
|
||||||
int next_c_straight = c + dir[dir_idx][1];
|
|
||||||
if (isValid(next_r_straight, next_c_straight) && g[next_r_straight][next_c_straight] == getExpectedValue(len + 1)) {
|
|
||||||
max_len_found = max(max_len_found, dfs_first_leg(g, next_r_straight, next_c_straight, len + 1, dir_idx));
|
|
||||||
}
|
|
||||||
|
|
||||||
return max_len_found;
|
|
||||||
}
|
|
||||||
|
|
||||||
int lenOfVDiagonal(const vector<vector<int>>& g) {
|
|
||||||
if (g.empty() || g[0].empty()) return 0;
|
|
||||||
n = g.size();
|
|
||||||
m = g[0].size();
|
|
||||||
|
|
||||||
int ans = 0;
|
|
||||||
bool has_one = false;
|
|
||||||
|
|
||||||
for (int i = 0; i < n; ++i) {
|
|
||||||
for (int j = 0; j < m; ++j) {
|
|
||||||
if (g[i][j] == 1) {
|
|
||||||
has_one = true;
|
|
||||||
for (int k = 0; k < 4; ++k) {
|
|
||||||
ans = max(ans, dfs_first_leg(g, i, j, 1, k));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (has_one && ans == 0) return 1;
|
|
||||||
|
|
||||||
return ans;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
Solution s;
|
|
||||||
int ans = s.lenOfVDiagonal({{2,2,1,2,2},{2,0,2,2,0},{2,0,1,1,0},{1,0,2,2,2},{2,0,0,2,2}});
|
|
||||||
cout<<ans<<"\n";
|
|
||||||
}
|
|
||||||
@ -1,93 +1,81 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stack>
|
#include <istream>
|
||||||
|
#include <ostream>
|
||||||
|
#include <tuple>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using pii = std::pair<int, int>;
|
/*
|
||||||
using ll = long long;
|
|
||||||
|
|
||||||
int main() {
|
dp[i][j]=字符串A前i个与字符串B前j个
|
||||||
std::ios_base::sync_with_stdio(false);
|
5
|
||||||
|
1 4 2 5 -12
|
||||||
|
4
|
||||||
|
-12 1 2 4
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
using ll = int64_t;
|
||||||
|
|
||||||
|
#define gdp(i,j,k)(std::get<k>(dp[i][j]))
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
std::iostream::sync_with_stdio(false);
|
||||||
std::cin.tie(nullptr);
|
std::cin.tie(nullptr);
|
||||||
|
ll n,m;
|
||||||
int n, m;
|
std::cin>>n;
|
||||||
std::cin >> n;
|
std::vector<ll> a(n+1);
|
||||||
std::vector<ll> a(n + 1);
|
for(ll i=1;i<=n;i++)std::cin>>a[i];
|
||||||
for (int i = 1; i <= n; ++i)
|
std::cin>>m;
|
||||||
std::cin >> a[i];
|
std::vector<ll> b(m+1);
|
||||||
|
for(ll j=1;j<=m;j++)std::cin>>b[j];
|
||||||
std::cin >> m;
|
std::vector<std::vector<std::tuple<ll,ll,ll>>>dp(n+1,std::vector<std::tuple<ll,ll,ll>>(m+1));
|
||||||
std::vector<ll> b(m + 1);
|
for(ll i=1;i<=n;i++){
|
||||||
for (int i = 1; i <= m; ++i)
|
for(ll j=1;j<=m;j++){
|
||||||
std::cin >> b[i];
|
if(a[i]!=b[j]){
|
||||||
|
gdp(i, j, 0) = gdp(i-1,j,0);
|
||||||
std::vector<std::vector<int>> dp(n + 1, std::vector<int>(m + 1, 0));
|
gdp(i, j, 1)=i-1;
|
||||||
|
gdp(i, j, 2)=j;
|
||||||
std::vector<std::vector<pii>> path(n + 1, std::vector<pii>(m + 1, {0, 0}));
|
}else{
|
||||||
|
ll maxprev=0;
|
||||||
for (int i = 1; i <= n; ++i) {
|
for(ll k=1;k<j;k++){
|
||||||
|
if(b[k]<a[i]){
|
||||||
int maxlen = 0;
|
maxprev=std::max(maxprev,gdp(i-1,k,0));
|
||||||
|
gdp(i,j,1)=i-1;
|
||||||
int pk = 0;
|
gdp(i,j,2)=k;
|
||||||
|
|
||||||
for (int j = 1; j <= m; ++j) {
|
|
||||||
|
|
||||||
dp[i][j] = dp[i - 1][j];
|
|
||||||
path[i][j] = {i - 1, j};
|
|
||||||
|
|
||||||
if (a[i] == b[j]) {
|
|
||||||
if (maxlen + 1 > dp[i][j]) {
|
|
||||||
dp[i][j] = maxlen + 1;
|
|
||||||
path[i][j] = {i - 1, pk};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
gdp(i,j,0)=maxprev+1;
|
||||||
if (b[j] < a[i]) {
|
|
||||||
if (dp[i - 1][j] > maxlen) {
|
|
||||||
maxlen = dp[i - 1][j];
|
|
||||||
pk = j;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
ll ans=0,maxi=0,maxj=0;
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
int maxlen = 0;
|
for(ll j=1;j<=m;j++){
|
||||||
int endj = 0;
|
// printf("dp[%lld][%lld]=%lld\n",i,j,dp[i][j]);
|
||||||
|
if(ans<gdp(i,j,0)){
|
||||||
for (int j = 1; j <= m; ++j) {
|
ans=gdp(i,j,0);
|
||||||
if (dp[n][j] > maxlen) {
|
maxi=i;
|
||||||
maxlen = dp[n][j];
|
maxj=j;
|
||||||
endj = j;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << maxlen << "\n";
|
|
||||||
|
|
||||||
if (maxlen > 0) {
|
|
||||||
std::stack<ll> res;
|
|
||||||
int curri = n;
|
|
||||||
int currj = endj;
|
|
||||||
|
|
||||||
while (curri > 0 && currj > 0) {
|
|
||||||
pii prev = path[curri][currj];
|
|
||||||
|
|
||||||
if (dp[curri][currj] > dp[prev.first][prev.second] && prev.second != currj) {
|
|
||||||
res.push(a[curri]);
|
|
||||||
}
|
}
|
||||||
|
std::vector<ll> ans2;
|
||||||
curri = prev.first;
|
ans2.reserve(n);
|
||||||
currj = prev.second;
|
// printf("maxi=%lld,maxj=%lld\n",maxi,maxj);
|
||||||
|
while(maxi>0){
|
||||||
|
ans2.push_back(a[maxi]);
|
||||||
|
ll tmpi=maxi;
|
||||||
|
maxi=gdp(maxi,maxj,1);
|
||||||
|
maxj=gdp(tmpi,maxj,2);
|
||||||
}
|
}
|
||||||
|
std::cout<<ans<<"\n";
|
||||||
bool first = true;
|
std::reverse(ans2.begin(),ans2.end());
|
||||||
while (!res.empty()) {
|
for(auto i:ans2){
|
||||||
if (!first) {
|
std::cout<<i<<" ";
|
||||||
std::cout << " ";
|
|
||||||
}
|
|
||||||
std::cout << res.top();
|
|
||||||
res.pop();
|
|
||||||
first = false;
|
|
||||||
}
|
|
||||||
std::cout << "\n";
|
|
||||||
}
|
}
|
||||||
|
std::cout<<std::endl;
|
||||||
|
_Exit(0);
|
||||||
}
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
int main(){
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,67 +0,0 @@
|
|||||||
/*
|
|
||||||
|
|
||||||
dpmax[i][j]=编号[i,j]合并后的最大值
|
|
||||||
dpmin[i][j]=编号[i,j]合并后的最小值
|
|
||||||
|
|
||||||
if op[k] == +
|
|
||||||
dpmax[i][j]=max(dpmax[i][k]+dpmax[k+1][j])
|
|
||||||
dpmin[i][j]=min(dpmin[i][k]+dpmin[k+1][j])
|
|
||||||
|
|
||||||
else if op[k] == *
|
|
||||||
dpmax[i][j]=max(dpmax[i][k]*dpmax[k+1][j],
|
|
||||||
dpmax[i][k]*dpmin[k+1][j],
|
|
||||||
dpmin[i][k]*dpmax[k+1][j],
|
|
||||||
dpmin[i][k]*dpmin[k+1][j])
|
|
||||||
|
|
||||||
dpmin[i][j]=min(dpmax[i][k]*dpmax[k+1][j],
|
|
||||||
dpmax[i][k]*dpmin[k+1][j],
|
|
||||||
dpmin[i][k]*dpmax[k+1][j],
|
|
||||||
dpmin[i][k]*dpmin[k+1][j])
|
|
||||||
|
|
||||||
dpmax[i][j] = -1e9
|
|
||||||
dpmin[i][j] = 1e9
|
|
||||||
|
|
||||||
dp[i][i]=arr[i]
|
|
||||||
|
|
||||||
*/
|
|
||||||
#include <cstdint>
|
|
||||||
#include <iostream>
|
|
||||||
#include <istream>
|
|
||||||
#include <vector>
|
|
||||||
using ll = int64_t;
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
std::iostream::sync_with_stdio(false);
|
|
||||||
std::cin.tie(nullptr);
|
|
||||||
|
|
||||||
ll n;
|
|
||||||
std::cin>>n;
|
|
||||||
const ll add=0,mul=1;
|
|
||||||
const ll n21=2*n+1;
|
|
||||||
std::vector<std::vector<ll>> op(n*2+1,std::vector<ll>(2));
|
|
||||||
for(ll i=1;i<=n;i++){
|
|
||||||
char c;
|
|
||||||
std::cin>>c;
|
|
||||||
if(c=='t'){
|
|
||||||
op[i][0]=add;
|
|
||||||
}else{
|
|
||||||
op[i][0]=mul;
|
|
||||||
}
|
|
||||||
std::cin>>op[i][1];
|
|
||||||
op[i+n]=op[i];
|
|
||||||
}
|
|
||||||
std::vector<std::vector<ll>> dpmax,dpmin;
|
|
||||||
for(ll s=1;s<n;s++){
|
|
||||||
ll e = s+n-1;
|
|
||||||
dpmax.clear();
|
|
||||||
dpmax.resize(n21,std::vector<ll>(n21,-1e9));
|
|
||||||
dpmin.clear();
|
|
||||||
dpmin.resize(n21,std::vector<ll>(n21,1e9));
|
|
||||||
for(ll i=s;i<=e;i++){
|
|
||||||
dpmax[i][i]=op[i][1];
|
|
||||||
}
|
|
||||||
for(ll i=s;i<=e;i++){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,20 +0,0 @@
|
|||||||
/*
|
|
||||||
|
|
||||||
dp[i][j]=前i个数字添加j个乘号的最大值
|
|
||||||
dp[i][j]=max(
|
|
||||||
dp[i][j],
|
|
||||||
dp[k][j-1] * num[k+1][i]
|
|
||||||
)
|
|
||||||
|
|
||||||
num[i][i]=n[i]-'0'
|
|
||||||
num[i][j]=num[i][j-1]*10+(n[j]-'0')
|
|
||||||
|
|
||||||
dp[i][j] = -1e9
|
|
||||||
dp[i][0] = num[1][i]
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,59 +0,0 @@
|
|||||||
#include <algorithm>
|
|
||||||
#include <functional>
|
|
||||||
#include <vector>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
#define pg(g)do{\
|
|
||||||
for(auto&i:(g)){\
|
|
||||||
for(auto&j:i){\
|
|
||||||
cout<<j<<",";\
|
|
||||||
}\
|
|
||||||
cout<<"\n";\
|
|
||||||
}\
|
|
||||||
}while(0)
|
|
||||||
|
|
||||||
class Solution {
|
|
||||||
public:
|
|
||||||
vector<vector<int>> sortMatrix(vector<vector<int>>& g) {
|
|
||||||
vector<int>p;
|
|
||||||
for(int i=0;i<g.size();++i){
|
|
||||||
int x=i,y=0;
|
|
||||||
p.clear();
|
|
||||||
do {
|
|
||||||
p.push_back(g[x][y]);
|
|
||||||
++x,++y;
|
|
||||||
}while (x<g.size()&&y<g[0].size());
|
|
||||||
sort(p.begin(),p.end(),greater<>());
|
|
||||||
x=i,y=0;
|
|
||||||
int tot=0;
|
|
||||||
do {
|
|
||||||
g[x][y]=p[tot++];
|
|
||||||
++x,++y;
|
|
||||||
}while (x<g.size()&&y<g[0].size());
|
|
||||||
}
|
|
||||||
// pg(g);
|
|
||||||
for (int j=1; j<g[0].size(); ++j) {
|
|
||||||
int x=0,y=j;
|
|
||||||
p.clear();
|
|
||||||
do{
|
|
||||||
p.push_back(g[x][y]);
|
|
||||||
++x;++y;
|
|
||||||
}while(x<g.size()&&y<g[0].size());
|
|
||||||
sort(p.begin(),p.end());
|
|
||||||
x=0,y=j;
|
|
||||||
int tot=0;
|
|
||||||
do {
|
|
||||||
g[x][y]=p[tot++];
|
|
||||||
++x,++y;
|
|
||||||
}while (x<g.size()&&y<g[0].size());
|
|
||||||
}
|
|
||||||
// pg(g);
|
|
||||||
return g;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
Solution s;
|
|
||||||
vector<vector<int>> v={{1,7,3},{5,8,2},{4,9,6}};
|
|
||||||
s.sortMatrix(v);
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user