Skip to content

Commit

Permalink
tcirc APCS
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyfs committed Sep 12, 2021
1 parent f970353 commit 7f71412
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tcirc/d029.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <set>
#include <utility>
#include <cstring>
#define Hirasawa_Yui_My_Wife ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pii pair<int,int>
#define F first
#define S second
#define ll long long
using namespace std;
int const N=2e5+10,INF=8e7;
int n;
int a[N],b[N];
int main(){
Hirasawa_Yui_My_Wife
cin>>n;
for(int i=0;i<n;++i)cin>>a[i];
for(int i=0;i<n;++i)cin>>b[i];
set<pii> st;
st.insert({INF,-1});
ll ans=0;
for(int i=0;i<n;++i){
auto big=st.upper_bound({a[i]+b[i],INF});
ans+=i-big->S-1;
while(!st.empty()&&st.begin()->F<=a[i])st.erase(st.begin());
st.insert({a[i],i});
}
cout<<ans<<'\n';
}
//set
39 changes: 39 additions & 0 deletions tcirc/d030.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <stack>
#define Hirasawa_Yui_My_Wife ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int const N=1e5+10,INF=8e7*2.5e1;
int n,l;
int c[N],h[N];
int main(){
Hirasawa_Yui_My_Wife
cin>>n>>l;
for(int i=1;i<=n;++i)cin>>c[i];
for(int i=1;i<=n;++i)cin>>h[i];
c[0]=0;
c[n+1]=l;
h[0]=h[n+1]=INF;
stack<int> stk;
stk.push(0);
int tot=0,mx=-INF;
for(int i=1;i<=n;++i){
while(c[stk.top()]+h[stk.top()]<=c[i]){
mx=max(mx,h[stk.top()]);
++tot;
stk.pop();
}
if(c[stk.top()]<=c[i]-h[i]||c[i]+h[i]<=c[i+1]){//tree i fell
mx=max(mx,h[i]);
++tot;
}else{
stk.push(i);
}
}
while(c[stk.top()]+h[stk.top()]<l){
mx=max(mx,h[stk.top()]);
++tot;
stk.pop();
}
cout<<tot<<'\n'<<mx<<'\n';
}
//greedy好題
52 changes: 52 additions & 0 deletions tcirc/d082.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <cstring>
#define INF 80000000
#define ll long long
#define Hirasawa_Yui_My_Wife ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int const N=30+5;
int n,m;
int a[N][N],dp[N][N][N][N];//dp[i][j][k][l]:=start at [i][j], (k+1)*(l+1) size
int xc[N][N],yc[N][N];//xc[i][j]:=number of 0s in prefix along x-axis ends at [i][j]
int mod(int to,int z){
return min(z,to-z);
}
int main(){
Hirasawa_Yui_My_Wife
cin>>n>>m;
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)cin>>a[i][j];
for(int x=1;x<=n;++x){
yc[x][0]=0;
yc[x][1]=(a[x][1]==0);
for(int y=2;y<=m;++y){
yc[x][y]=yc[x][y-1]+(a[x][y]==0);
}
}
for(int y=1;y<=m;++y){
xc[0][y]=0;
xc[1][y]=(a[1][y]==0);
for(int x=2;x<=n;++x){
xc[x][y]=xc[x-1][y]+(a[x][y]==0);
}
}
memset(dp,0,sizeof(dp));
for(int dx=1;dx<n;++dx){
for(int dy=1;dy<m;++dy){
for(int x=1;x+dx<=n;++x){
for(int y=1;y+dy<=m;++y){
dp[x][y][dx][dy]=INF;
//up
dp[x][y][dx][dy]=min(dp[x][y][dx][dy],mod(dy+1,yc[x][y+dy]-yc[x][y-1])+dp[x+1][y][dx-1][dy]);
//down
dp[x][y][dx][dy]=min(dp[x][y][dx][dy],mod(dy+1,yc[x+dx][y+dy]-yc[x+dx][y-1])+dp[x][y][dx-1][dy]);
//left
dp[x][y][dx][dy]=min(dp[x][y][dx][dy],mod(dx+1,xc[x+dx][y]-xc[x-1][y])+dp[x][y+1][dx][dy-1]);
//right
dp[x][y][dx][dy]=min(dp[x][y][dx][dy],mod(dx+1,xc[x+dx][y+dy]-xc[x-1][y+dy])+dp[x][y][dx][dy-1]);
}
}
}
}
cout<<dp[1][1][n-1][m-1]<<'\n';
}
//噁心的四維區間dp
37 changes: 37 additions & 0 deletions tcirc/d094.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <queue>
#include <utility>
#include <cstring>
#define Hirasawa_Yui_My_Wife ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pii pair<int,int>
#define F first
#define S second
using namespace std;
int const N=1e6+10;
int n,p,l,r,nxt[N];
bool vis[N];
int main(){
Hirasawa_Yui_My_Wife
cin>>n>>p>>l>>r;
for(int i=0;i<n;++i)cin>>nxt[i];
memset(vis,0,sizeof(vis));
vis[0]=1;
queue<pii> que;
que.push({0,0});
int ans=-1;
while(!que.empty()){
pii cur=que.front();
if(cur.F==p){ans=cur.S;break;}
if(cur.F+r<n&&0<=nxt[cur.F+r]&&nxt[cur.F+r]<n&&!vis[cur.F+r]){
vis[nxt[cur.F+r]]=vis[cur.F+r]=1;
que.push({nxt[cur.F+r],cur.S+1});
}
if(0<=cur.F-l&&0<=nxt[cur.F-l]&&nxt[cur.F-l]<n&&!vis[cur.F-l]){
vis[nxt[cur.F-l]]=vis[cur.F-l]=1;
que.push({nxt[cur.F-l],cur.S+1});
}
que.pop();
}
cout<<ans<<'\n';
}
//bfs最短路徑
37 changes: 37 additions & 0 deletions tcirc/d105.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <cstring>
#define ll long long
#define Hirasawa_Yui_My_Wife ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int const N=1e5+10,M=1e2+3;
int n,m;
int w[2*N],op[M],l[N],r[N];
void build(int cur){
if(cur>=n)return;
build(l[cur]);
build(r[cur]);
w[cur]=w[l[cur]]+w[r[cur]];
}
int put(int o){
int cur=1;
while(cur<n){
w[cur]+=o;
cur=(w[l[cur]]<=w[r[cur]])?l[cur]:r[cur];
}
w[cur]+=o;
return cur;
}
int main(){
Hirasawa_Yui_My_Wife
cin>>n>>m;
for(int i=n;i<2*n;++i)cin>>w[i];
for(int i=0;i<m;++i)cin>>op[i];
int q,s,t;
for(int i=0;i<n-1;++i)cin>>q>>s>>t,l[q]=s,r[q]=t;
build(1);
for(int i=0;i<m;++i){
cout<<put(op[i])<<' ';
}
cout<<'\n';
}
//二分搜尋樹實作

0 comments on commit 7f71412

Please sign in to comment.