mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-11-06 06:43:49 +00:00
refactor: 重构并移动代码文件位置
将P11233.cpp从7/28目录移动到10/28目录并实现新功能 添加P9753.cpp解决括号匹配计数问题
This commit is contained in:
parent
e48cc83bfb
commit
4e419a46c7
40
src/10/27/P9753.cpp
Normal file
40
src/10/27/P9753.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
using ll = int64_t;
|
||||
|
||||
ll n,ans;
|
||||
std::string s;
|
||||
const ll maxn = 2e6+5;
|
||||
ll dp[maxn],end[maxn];
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin>>n;
|
||||
std::cin>>s;
|
||||
s='\0'+s;
|
||||
struct S{
|
||||
ll num,pos,ans;
|
||||
};
|
||||
std::stack<S> stk;
|
||||
for(ll i=1;i<=n;i++){
|
||||
if(stk.size() && stk.top().num==s[i]){
|
||||
if(stk.size()==1){
|
||||
end[i]+=end[stk.top().pos-1]*(ans-stk.top().ans);
|
||||
ans+=end[i];
|
||||
}
|
||||
ans+=1;
|
||||
end[i]=ans;
|
||||
stk.pop();
|
||||
}else{
|
||||
stk.emplace(s[i],i,ans);
|
||||
}
|
||||
printf("i=%lld, ans=%lld, end=%lld\n",i,ans,end[i]);
|
||||
}
|
||||
std::cout<<ans<<"\n";
|
||||
}
|
||||
53
src/10/28/P11233.cpp
Normal file
53
src/10/28/P11233.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
|
||||
i=a[j]前面最近相同数字的下标
|
||||
dp[j][0]=dp[i+1][0]+same_color[j]-same_color[i]+a[j]
|
||||
, dp[j-1][0]
|
||||
|
||||
*/
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
using ll = int64_t;
|
||||
|
||||
const ll maxn=2e6+5;
|
||||
ll n,a[maxn],sc[maxn],si[maxn],dp[maxn];
|
||||
|
||||
#define printf
|
||||
|
||||
static inline void solve(){
|
||||
std::cin>>n;
|
||||
for(ll i=0;i<maxn;i++){
|
||||
si[i]=0;
|
||||
dp[i]=0;
|
||||
}
|
||||
for(ll i=1;i<=n;i++){
|
||||
std::cin>>a[i];
|
||||
sc[i]=sc[i-1]+(a[i]==a[i-1]?a[i]:0);
|
||||
printf("sc[%lld]=%lld\n",i,sc[i]);
|
||||
}
|
||||
for(ll j=1;j<=n;j++){
|
||||
const ll i=si[a[j]];
|
||||
dp[j]=dp[j-1];
|
||||
if(i>0){
|
||||
dp[j]=std::max(
|
||||
dp[i+1]+(sc[j]-sc[i+1])/* 为什么这里不能是sc[j-1]-sc[i+1] */+a[j],
|
||||
dp[j]
|
||||
);
|
||||
}
|
||||
printf("i=%lld, dp[%lld]=%lld\n",i,j,dp[j]);
|
||||
si[a[j]]=j;
|
||||
}
|
||||
std::cout<<dp[n]<<"\n";
|
||||
}
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
ll T;
|
||||
std::cin>>T;
|
||||
while(T--)solve();
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
int main(){
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user