mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-12-16 12:13:03 +00:00
feat: 添加多个算法题目解决方案
添加P5542.cpp、P1115.cpp、P6100.cpp和P4188.cpp的解决方案 更新P2258.cpp的实现,增加矩阵处理逻辑
This commit is contained in:
parent
5b166bcd0e
commit
44be3ee092
19
src/11/11/P1115.cpp
Normal file
19
src/11/11/P1115.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
using ll = int64_t;
|
||||
|
||||
const ll inf=1e9+7;
|
||||
ll n;
|
||||
|
||||
int main(){
|
||||
std::cin>>n;
|
||||
ll max=-inf,nmax=-inf;
|
||||
for(ll i=1;i<=n;i++){
|
||||
ll tmp;
|
||||
std::cin>>tmp;
|
||||
nmax=std::max(tmp,nmax+tmp);
|
||||
max=std::max(max,nmax);
|
||||
}
|
||||
std::cout<<max<<"\n";
|
||||
}
|
||||
84
src/11/11/P4188.cpp
Normal file
84
src/11/11/P4188.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
const int MAXN = 100010;
|
||||
|
||||
struct Event {
|
||||
int time;
|
||||
int id;
|
||||
bool isStart;
|
||||
|
||||
bool operator<(const Event& other) const {
|
||||
if (time != other.time) return time < other.time;
|
||||
return isStart > other.isStart; // 结束事件优先处理
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
ios_base::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vector<pair<int, int>> intervals(n);
|
||||
vector<Event> events;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int start, end;
|
||||
cin >> start >> end;
|
||||
intervals[i] = {start, end};
|
||||
events.push_back({start, i, true});
|
||||
events.push_back({end, i, false});
|
||||
}
|
||||
|
||||
sort(events.begin(), events.end());
|
||||
|
||||
// 计算总覆盖时间和每个救生员的单独贡献
|
||||
int totalCover = 0;
|
||||
vector<int> aloneTime(n, 0);
|
||||
|
||||
int activeCount = 0;
|
||||
int lastTime = 0;
|
||||
int singleActive = -1; // 当只有一个救生员活跃时,记录其ID
|
||||
|
||||
for (const Event& e : events) {
|
||||
if (activeCount > 0) {
|
||||
totalCover += e.time - lastTime;
|
||||
}
|
||||
|
||||
if (activeCount == 1) {
|
||||
// 只有一个救生员活跃时,这段时间完全属于这个救生员
|
||||
aloneTime[singleActive] += e.time - lastTime;
|
||||
}
|
||||
|
||||
if (e.isStart) {
|
||||
activeCount++;
|
||||
} else {
|
||||
activeCount--;
|
||||
}
|
||||
|
||||
// 更新singleActive
|
||||
if (activeCount == 1) {
|
||||
// 找到当前活跃的救生员
|
||||
singleActive = e.id;
|
||||
} else {
|
||||
singleActive = -1;
|
||||
}
|
||||
|
||||
lastTime = e.time;
|
||||
}
|
||||
|
||||
// 找到aloneTime的最小值
|
||||
int minAlone = 1e9;
|
||||
for (int i = 0; i < n; i++) {
|
||||
minAlone = min(minAlone, aloneTime[i]);
|
||||
}
|
||||
|
||||
// 结果是总覆盖时间减去最小的单独贡献
|
||||
int result = totalCover - minAlone;
|
||||
cout << result << endl;
|
||||
|
||||
}
|
||||
3
src/11/11/P5542.cpp
Normal file
3
src/11/11/P5542.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
int main(){
|
||||
|
||||
}
|
||||
50
src/11/11/P6100.cpp
Normal file
50
src/11/11/P6100.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
using ll = int64_t;
|
||||
|
||||
const ll maxn = 205, inf=1e9+7;
|
||||
ll n,k,pre[maxn][maxn],dp[maxn][maxn],ans=-inf,ud[maxn][maxn][maxn];
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin>>n>>k;
|
||||
for(ll i=1;i<=n;i++){
|
||||
ll x1,y1,x2,y2;
|
||||
std::cin>>x1>>y1>>x2>>y2;
|
||||
pre[x1][y1]++,pre[x2+1][y2+1]++;
|
||||
pre[x2+1][y1]--,pre[x1][y2+1]--;
|
||||
}
|
||||
for(ll i=1;i<maxn;i++){
|
||||
for(ll j=1;j<maxn;j++){
|
||||
dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+pre[i][j];
|
||||
}
|
||||
}
|
||||
for(ll l=1;l<maxn;l++){
|
||||
for(ll u=1;u<maxn-1;u++){
|
||||
for(ll d=u;d<maxn;d++){
|
||||
ud[l][u][d]=ud[l][u][d-1]+dp[d][l];
|
||||
}
|
||||
}
|
||||
}
|
||||
// for(ll i=1;i<=10;i++){
|
||||
// for(ll j=1;j<=10;j++){
|
||||
// printf("%lld ",ud[j][i][i+1]);
|
||||
// }
|
||||
// printf("\n");
|
||||
// }
|
||||
for(ll u=1;u<maxn-1;u++){
|
||||
for(ll d=u;d<maxn;d++){
|
||||
ll nmax=-inf;
|
||||
for(ll i=1;i<maxn;i++){
|
||||
nmax=std::max(ud[i][u][d],nmax+ud[i][u][d]);
|
||||
ans=std::max(ans,nmax);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout<<ans<<"\n";
|
||||
}
|
||||
@ -1,3 +1,56 @@
|
||||
int main(){
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
using ll = int64_t;
|
||||
#define sl static inline
|
||||
const ll maxn = 18;
|
||||
ll n,m,r,c;
|
||||
ll a[maxn][maxn],dp[maxn][maxn],h[maxn],ans=1e9+7,lr[maxn][maxn],ud[maxn];
|
||||
|
||||
sl void solve(){
|
||||
printf("h[]= ");
|
||||
for(ll i=1;i<=r;i++){
|
||||
printf("%lld ",h[i]);
|
||||
}
|
||||
printf("\n");
|
||||
for(ll l=1;l<m;l++){
|
||||
for(ll l2=l+1;l2<=m;l2++){
|
||||
for(ll i=1;i<=r;i++){
|
||||
lr[l][l2]+=std::abs(a[h[i]][l]-a[h[i]][l2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
for(ll l=1;l<=m;l++){
|
||||
for(ll i=2;i<=r;i++){
|
||||
ud[l]+=std::abs(a[h[i-1]][l]-a[h[i]][l]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
sl void dfs(ll start,ll depth){
|
||||
// printf("dfs start%lld, depth=%lld\n",start,depth);
|
||||
if(depth>r){
|
||||
solve();
|
||||
return;
|
||||
}
|
||||
for(ll i=start;i<=n-(r-depth);i++){
|
||||
h[depth]=i;
|
||||
dfs(i+1,depth+1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin>>n>>m>>r>>c;
|
||||
for(ll i=1;i<=n;i++){
|
||||
for(ll j=1;j<=m;j++){
|
||||
std::cin>>a[i][j];
|
||||
}
|
||||
}
|
||||
dfs(1, 1);
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user