mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-10-29 11:33:03 +00:00
Compare commits
3 Commits
59ad401d66
...
385380934e
| Author | SHA1 | Date | |
|---|---|---|---|
| 385380934e | |||
| 056863889b | |||
|
|
6c7412f1d2 |
164
src/10/5/P2491.cpp
Normal file
164
src/10/5/P2491.cpp
Normal file
@ -0,0 +1,164 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <deque>
|
||||
|
||||
using namespace std;
|
||||
|
||||
using ll = long long;
|
||||
const int MAXN = 300005;
|
||||
const ll INF = 4e18;
|
||||
|
||||
struct Edge {
|
||||
int to;
|
||||
int weight;
|
||||
};
|
||||
|
||||
vector<Edge> adj[MAXN];
|
||||
int n;
|
||||
ll s;
|
||||
|
||||
ll dist[MAXN];
|
||||
int p[MAXN];
|
||||
|
||||
int bfs(int start, int N, int& farnds) {
|
||||
fill(dist + 1, dist + N + 1, -1);
|
||||
fill(p + 1, p + N + 1, 0);
|
||||
deque<int> q;
|
||||
|
||||
dist[start] = 0;
|
||||
q.push_back(start);
|
||||
|
||||
farnds = start;
|
||||
while (!q.empty()) {
|
||||
int u = q.front();
|
||||
q.pop_front();
|
||||
if (dist[u] > dist[farnds]) {
|
||||
farnds = u;
|
||||
}
|
||||
for (const auto& edge : adj[u]) {
|
||||
int v = edge.to;
|
||||
int w = edge.weight;
|
||||
if (dist[v] == -1) {
|
||||
dist[v] = dist[u] + w;
|
||||
p[v] = u;
|
||||
q.push_back(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
return farnds;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ll bdist[MAXN];
|
||||
bool d[MAXN];
|
||||
|
||||
void dfs(int u, int p) {
|
||||
bdist[u] = 0;
|
||||
for (const auto& edge : adj[u]) {
|
||||
int v = edge.to;
|
||||
int w = edge.weight;
|
||||
|
||||
if (v == p || d[v]) {
|
||||
continue;
|
||||
}
|
||||
dfs(v, u);
|
||||
|
||||
bdist[u] = max(bdist[u], bdist[v] + w);
|
||||
}
|
||||
}
|
||||
|
||||
void solve() {
|
||||
ios_base::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
if (!(cin >> n >> s)) return;
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
int u, v, w;
|
||||
if (!(cin >> u >> v >> w)) return;
|
||||
adj[u].push_back({v, w});
|
||||
adj[v].push_back({u, w});
|
||||
}
|
||||
if (n == 1) {
|
||||
cout << 0 << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
int A = 0, B = 0;
|
||||
|
||||
A = bfs(1, n, A);
|
||||
B = bfs(A, n, B);
|
||||
|
||||
vector<int> dpath;
|
||||
int curr = B;
|
||||
|
||||
while (curr != 0) {
|
||||
dpath.push_back(curr);
|
||||
d[curr] = true;
|
||||
curr = p[curr];
|
||||
}
|
||||
reverse(dpath.begin(), dpath.end());
|
||||
|
||||
int k = dpath.size();
|
||||
vector<ll> d(k);
|
||||
vector<ll> e(k);
|
||||
for (int i = 0; i < k; ++i) {
|
||||
d[i] = dist[dpath[i]];
|
||||
}
|
||||
for (int i = 0; i < k; ++i) {
|
||||
dfs(dpath[i], 0);
|
||||
e[i] = bdist[dpath[i]];
|
||||
}
|
||||
|
||||
vector<ll> L(k + 1, -INF);
|
||||
vector<ll> R(k + 1, -INF);
|
||||
|
||||
for (int i = 0; i < k; ++i) {
|
||||
L[i + 1] = max(L[i], e[i] - d[i]);
|
||||
}
|
||||
|
||||
for (int i = k - 1; i >= 0; --i) {
|
||||
R[i] = max(R[i + 1], e[i] + d[i]);
|
||||
}
|
||||
|
||||
ll ansdist = INF;
|
||||
deque<int> dq;
|
||||
int p1 = 0;
|
||||
|
||||
for (int p2 = 0; p2 < k; ++p2) {
|
||||
while (!dq.empty() && e[dq.back()] <= e[p2]) {
|
||||
dq.pop_back();
|
||||
}
|
||||
dq.push_back(p2);
|
||||
|
||||
while (d[p2] - d[p1] > s) {
|
||||
if (!dq.empty() && dq.front() == p1) {
|
||||
dq.pop_front();
|
||||
}
|
||||
p1++;
|
||||
}
|
||||
|
||||
ll maxmid = e[dq.front()];
|
||||
|
||||
ll maxleft = -INF;
|
||||
if (p1 > 0) {
|
||||
maxleft = L[p1] + d[p1];
|
||||
}
|
||||
|
||||
ll maxright = -INF;
|
||||
if (p2 < k - 1) {
|
||||
maxright = R[p2 + 1] - d[p2];
|
||||
}
|
||||
|
||||
|
||||
ll curmax = max({maxmid, maxleft, maxright});
|
||||
ansdist = min(ansdist, curmax);
|
||||
}
|
||||
|
||||
cout << ansdist << "\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
solve();
|
||||
}
|
||||
87
src/10/9/P8903.cpp
Normal file
87
src/10/9/P8903.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
using ll = int64_t;
|
||||
|
||||
const ll maxn = 2005;
|
||||
|
||||
struct C {
|
||||
ll p, c, x;
|
||||
bool operator<(const C &other) const {
|
||||
return x < other.x;
|
||||
}
|
||||
} c[maxn];
|
||||
|
||||
ll n, a, b;
|
||||
ll dpb[maxn][maxn], dpa[maxn][maxn];
|
||||
|
||||
ll mxb[maxn][maxn], mxa[maxn][maxn];
|
||||
|
||||
int main() {
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin >> n >> a >> b;
|
||||
for (ll i = 1; i <= n; i++) {
|
||||
std::cin >> c[i].p >> c[i].c >> c[i].x;
|
||||
}
|
||||
std::sort(c + 1, c + 1 + n);
|
||||
|
||||
for (ll i = 1; i <= n; i++) {
|
||||
for (ll j = 0; j <= b; j++) {
|
||||
dpb[i][j] = dpb[i - 1][j];
|
||||
if (j >= c[i].c * c[i].x) {
|
||||
dpb[i][j] = std::max(dpb[i][j], dpb[i - 1][j - c[i].c * c[i].x] + c[i].p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (ll i = 1; i <= n; i++) {
|
||||
for (ll j = 0; j <= b; j++) {
|
||||
if (j > 0) {
|
||||
mxb[i][j] = std::max(mxb[i][j - 1], dpb[i][j]);
|
||||
} else {
|
||||
mxb[i][j] = dpb[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (ll i = n; i >= 1; i--) {
|
||||
for (ll j = 0; j <= a; j++) {
|
||||
dpa[i][j] = dpa[i + 1][j];
|
||||
if (j >= c[i].c) {
|
||||
dpa[i][j] = std::max(dpa[i][j], dpa[i + 1][j - c[i].c] + c[i].p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (ll i = n; i >= 1; i--) {
|
||||
for (ll j = 0; j <= a; j++) {
|
||||
if (j > 0) {
|
||||
mxa[i][j] = std::max(mxa[i][j - 1], dpa[i][j]);
|
||||
} else {
|
||||
mxa[i][j] = dpa[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
|
||||
ans = std::max(ans, mxb[n][b]);
|
||||
ans = std::max(ans, mxa[1][a]);
|
||||
|
||||
for (ll i = 1; i <= n; i++) {
|
||||
for (ll j = 0; j <= c[i].c; j++) {
|
||||
ll mcost = c[i].c - j;
|
||||
ll iccost = j * c[i].x;
|
||||
if (mcost <= a && iccost <= b) {
|
||||
|
||||
ll pre = (i > 1) ? mxb[i - 1][b - iccost] : 0;
|
||||
ll suff = (i < n) ? mxa[i + 1][a - mcost] : 0;
|
||||
ans = std::max(ans, pre + c[i].p + suff);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << ans << "\n";
|
||||
}
|
||||
29
src/10/9/mn1009t1.cpp
Normal file
29
src/10/9/mn1009t1.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
using ll = int64_t;
|
||||
|
||||
const ll maxn = 1e6+5;
|
||||
ll n, a[maxn], maxi,mini;
|
||||
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin>>n;
|
||||
for(ll i=1;i<=n;i++){
|
||||
std::cin>>a[i];
|
||||
}
|
||||
for(ll i=2;i<=n;i++){
|
||||
ll _;
|
||||
std::cin>>_;
|
||||
}
|
||||
maxi=a[1],mini=a[1];
|
||||
for(ll i=2;i<=n;i++){
|
||||
maxi=std::max(maxi,a[i]);
|
||||
mini=std::min(mini,a[i]);
|
||||
std::cout<<maxi-mini<<"\n";
|
||||
}
|
||||
}
|
||||
15
src/test.cpp
15
src/test.cpp
@ -1,3 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
using uint64 = unsigned long long;
|
||||
|
||||
void (*ptr)(void) = (void(*)(void))0xDEADBEEF;
|
||||
|
||||
int main(){
|
||||
|
||||
uint64 nptr =(uint64)(printf);
|
||||
printf("func ptr= %lx\n",printf);
|
||||
ptr=(void(*)(void))nptr;
|
||||
ptr();
|
||||
ptr();
|
||||
ptr();
|
||||
ptr();
|
||||
ptr();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user