Algo-Trian
This commit is contained in:
22
Algorithm/DP-DynamicProgramming/Linear-DP/P1115 最大子段和.cpp
Normal file
22
Algorithm/DP-DynamicProgramming/Linear-DP/P1115 最大子段和.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <climits>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int INF = INT_MAX >> 1;
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
vector<int> a(n);
|
||||
vector<int> dp(n + 1, -INF);
|
||||
for (int i = 0; i < n; i++) {
|
||||
cin >> a[i];
|
||||
}
|
||||
dp[0] = 0;
|
||||
for (int i = 1; i < n + 1; i++) {
|
||||
dp[i] = max(dp[i - 1] + a[i - 1], a[i - 1]);
|
||||
}
|
||||
cout << *max_element(dp.begin() + 1, dp.end()) << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
int main(){
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
string s;
|
||||
cin >> s;
|
||||
int n = s.size();
|
||||
vector<int> dp(n, 1);
|
||||
//dp[i]: <20><>i<EFBFBD><69>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EEB3A4><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><D3B4><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
//Ԥ<><D4A4><EFBFBD><EFBFBD>
|
||||
for(int i = 1; i < n; i++){
|
||||
if(s[i] == s[i - 1] + 1 || s[i] == s[i - 1]){
|
||||
dp[i] = dp[i - 1] + 1;
|
||||
}
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD>Ҫ<EFBFBD>ݳ⣬<DDB3><E2A3AC>ô<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD>
|
||||
int sum = 0;
|
||||
for(int i = 0; i < n; i++){
|
||||
//<2F>Լ<EFBFBD><D4BC><EFBFBD>
|
||||
sum += (dp[i] - 1) * dp[i] / 2;
|
||||
//ǰ<><C7B0>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
sum += dp[i] * (i - dp[i]);
|
||||
}
|
||||
cout << sum << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
//tuple(t, x, y)
|
||||
int dist(const tuple<int, int, int>& i, const tuple<int, int, int>& j){
|
||||
return ( abs(get<1>(i) - get<1>(j)) + abs(get<2>(i) - get<2>(j)) );
|
||||
}
|
||||
int time(const tuple<int, int, int>& i, const tuple<int, int, int>& j){
|
||||
return abs( get<0>(i) - get<0>(j) );
|
||||
}
|
||||
int main(){
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
//n * n, m <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
vector<tuple<int, int, int>> ham(m);
|
||||
vector<int> dp(m, 1);
|
||||
for(int i = 0; i < m; i++){
|
||||
int t, x, y;
|
||||
cin >> t >> x >> y;
|
||||
ham[i] = make_tuple(t, x, y);
|
||||
}
|
||||
//dp[m] = max (<28><><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD>(t2 - t1 > dist)dp[x] + 1)
|
||||
for(int i = 1; i < m; i++){
|
||||
for(int j = 0; j < i; j++){
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD><CDB8><EFBFBD>
|
||||
if(dist(ham[i], ham[j]) <= time(ham[i], ham[j])){
|
||||
dp[i] = max(dp[i], dp[j] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << *max_element(dp.begin(), dp.end()) << endl;
|
||||
return 0;
|
||||
}
|
||||
42
Algorithm/DP-DynamicProgramming/P1807 最长路.cpp
Normal file
42
Algorithm/DP-DynamicProgramming/P1807 最长路.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
const int NEG_INF = -1e9;
|
||||
|
||||
int main() {
|
||||
int v, e;
|
||||
cin >> v >> e;
|
||||
|
||||
vector<vector<int>> dp(v, vector<int>(v, NEG_INF));
|
||||
for (int i = 0; i < v; i++) dp[i][i] = 0; // <20>Ի<EFBFBD>=0
|
||||
|
||||
for (int i = 0; i < e; i++) {
|
||||
int a, b, w;
|
||||
cin >> a >> b >> w;
|
||||
dp[a - 1][b - 1] = max(dp[a - 1][b - 1], w); // <20><><EFBFBD>ܶ<EFBFBD><DCB6><EFBFBD><EFBFBD>ߣ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
||||
}
|
||||
|
||||
for (int k = 0; k < v; k++) {
|
||||
for (int i = 0; i < v; i++) {
|
||||
if (dp[i][k] <= NEG_INF / 2) continue;
|
||||
for (int j = 0; j < v; j++) {
|
||||
if (dp[k][j] <= NEG_INF / 2) continue;
|
||||
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int mx = -1;
|
||||
for (int i = 0; i < v; i++) {
|
||||
for (int j = 0; j < v; j++) {
|
||||
if (dp[i][j] > NEG_INF / 2) {
|
||||
mx = max(mx, dp[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mx == -1 ? cout << mx << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
61
Algorithm/DP-DynamicProgramming/P3916 图的遍历.cpp
Normal file
61
Algorithm/DP-DynamicProgramming/P3916 图的遍历.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int main(){
|
||||
//dpֻ<70><D6BB>O(n3)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bfs<66><73><EFBFBD><EFBFBD>dfs
|
||||
int v, e;
|
||||
cin >> v >> e;
|
||||
//vector<vector<int>> adj(v, vector<int>());
|
||||
vector<vector<bool>> dp(v, vector<bool>(v, false));
|
||||
for(int i = 0; i < e; i++){
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
//adj[a - 1].push_back(b - 1);
|
||||
//adj[b - 1].push_back(a - 1);
|
||||
dp[a - 1][b - 1] = true;
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD>dp[i][j] : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>k<EFBFBD>ķ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
|
||||
for (int i = 0; i < v; i++) dp[i][i] = true; // <20>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>ͨ
|
||||
for(int k = 0; k < v; k++){
|
||||
for(int i = 0; i < v; i++){
|
||||
for(int j = 0; j < v; j++){
|
||||
if(dp[i][j]) continue;
|
||||
dp[i][j] = dp[i][j] || (dp[i][k] & dp[k][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//bfs
|
||||
for(int i = 0; i < v; i++) {
|
||||
vector<bool> visited(v, false);
|
||||
queue<int> q;
|
||||
q.push(i);
|
||||
visited[i] = true;
|
||||
int mx = i;
|
||||
while (!q.empty()) {
|
||||
int curr = q.front();
|
||||
q.pop();
|
||||
for (int next : adj[curr]) {
|
||||
if (!visited[next]) {
|
||||
visited[next] = true;
|
||||
q.push(next);
|
||||
mx = max(mx, next);
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << mx + 1 << " ";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
for(int i = 0; i < v; i++){
|
||||
int mx = -1;
|
||||
for(int j = 0; j < v; j++){
|
||||
if(dp[i][j] && mx < j) mx = j;
|
||||
}
|
||||
cout << mx + 1 << " ";
|
||||
}
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user