复习2。
一.╥﹏╥...倒计时72天╥﹏╥...-CSDN博客
1.*
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e3+5;
const int inf=0x3f3f3f3f;
int a[N][N],b[N][N];
void solve()
{int n;cin>>n;for(int i=1;i<=n;i++){for(int j=1;j<=i;j++){cin>>a[i][j];}}b[1][1]=a[1][1];for(int i=2;i<=n;i++){for(int j=1;j<=i;j++){if(j==1)b[i][j]=b[i-1][j]+a[i][j];else if(j==i)b[i][j]=b[i-1][j-1]+a[i][j];else b[i][j]=max(b[i-1][j],b[i-1][j-1])+a[i][j];}}if(n&1)cout<<b[n][n/2+1];else cout<<max(b[n][n/2],b[n][n/2+1]);
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
2.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
const int inf=0x3f3f3f3f;
int a[N],b[N];
void solve()
{int n;cin>>n;for(int i=0;i<n;i++){cin>>a[i];b[i]=a[i];}sort(a,a+n);int p,q,r;if(n&1){p=a[n/2-1],q=a[n/2],r=a[n/2+1];//2,5,8}else{p=a[n/2-1],q=a[n/2];//2,5}for(int i=0;i<n;i++){if(n&1){if(b[i]<=p){printf("%.1f\n",(q+r)/2.0);}else if(b[i]>=r){printf("%.1f\n",(p+q)/2.0);}else printf("%.1f\n",(p+r)/2.0);}else{if(b[i]<=p){printf("%.1f\n",1.0*q);}else{printf("%.1f\n",1.0*p);}}}
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
3.*
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
const int inf=0x3f3f3f3f;
const int mod=1000000007;
int a[N],b[N];
void solve()
{int n;cin>>n;int ma,mb,sum=0;cin>>ma;for(int i=ma;i>0;i--){cin>>a[i];}cin>>mb;for(int i=mb;i>0;i--){cin>>b[i];}for(int i=ma;i>1;i--){int p=a[i-1]+1;int q=b[i-1]+1;int r=2;sum=(sum+a[i]-b[i])*(max({p,q,r}))%mod;}sum+=a[1]-b[1];cout<<sum;
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
4.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+5;
const int inf=0x3f3f3f3f;
int a[N],b[N],c[N];
void solve()
{int n,k,sum=0;cin>>n>>k;for(int i=0;i<n;i++)//早上{cin>>a[i];sum+=a[i];}for(int i=0;i<n;i++)//晚上{cin>>b[i];c[i]=b[i]-a[i];}sort(c,c+n);for(int i=n-1;i>=k;i--){sum+=c[i];}cout<<sum;
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
5.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e4+5;
const int inf=0x3f3f3f3f;
string s[30];
bool cmp(string s1,string s2)
{return s1+s2>s2+s1;
}
void solve()
{int n;cin>>n;for(int i=0;i<n;i++){cin>>s[i];}sort(s,s+n,cmp);for(int i=0;i<n;i++){cout<<s[i];}
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
6.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e4+5;
const int inf=0x3f3f3f3f;
stack<string>st;
void solve()
{string s;bool flag=1;cin>>s;for(int i=0;i<s.size();i++){if(s[i]=='a')st.push("a");else{if(st.empty()){flag=0;break;}else st.pop();}}if(!st.empty())flag=0;if(flag)cout<<"Good";else cout<<"Bad";
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
7.*
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e6+5;
const int inf=0x3f3f3f3f;
int a[N],b[N]={};
stack<int>st;
void solve()
{int n,cn=0,max1=-inf;cin>>n;for(int i=0;i<n;i++){cin>>a[i];}for(int i=n-1;i>=0;i--){max1=max(max1,a[i]);b[i]=max1;}for(int i=0;i<n;i++){st.push(a[i]);while(!st.empty()&&st.top()>b[i+1]){cn++;if(cn<n)cout<<st.top()<<' ';else cout<<st.top();st.pop();}}
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
8.*
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e4+5;
const int inf=0x3f3f3f3f;
stack<char>st;
void solve()
{string s;while(cin>>s){for(int i=0;i<s.size();i++){st.push(s[i]);while(st.size()>=2){int p=st.top();st.pop();int q=st.top();st.pop();if(p=='o'&&q=='o'){st.push('O');}else if(p!=q){st.push(q);st.push(p);break;//不break会无限循环下去}}}string a;while(!st.empty()){a+=st.top();st.pop();}reverse(a.begin(),a.end());cout<<a<<endl;}
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
二.倒计时71天-CSDN博客
1.*
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e4+5;
const int inf=0x3f3f3f3f;
void solve()
{int x,y,l,r;cin>>x>>y>>l>>r;int a=gcd(x,y);x/=a,y/=a;if(x>y)swap(x,y);int p=l/x;if(l%x)p+=1;int q=r/y;cout<<max(0ll,q-p+1);
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
2.*
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
vector<int>ve[N];
int dp[N][2];//0:不染,1:染
void dfs(int x,int fa)
{dp[x][0]=1,dp[x][1]=1;for(auto i:ve[x]){if(i==fa)continue;dfs(i,x);dp[x][0]=dp[x][0]*dp[i][1]%mod;dp[x][1]=dp[x][1]*(dp[i][0]+dp[i][1])%mod;}
}
void solve()
{int n;cin>>n;for(int i=1;i<n;i++){int u,v;cin>>u>>v;ve[u].push_back(v);ve[v].push_back(u);}dfs(1,0);cout<<(dp[1][0]+dp[1][1])%mod;
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
倒计时70天-CSDN博客
1.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e4+5;
const int inf=0x3f3f3f3f;
#define pb push_back
map<int,int>mp;
vector<int>ve;
bool cmp(int a,int b)
{return mp[a]>mp[b];
}
void solve()
{int n,m,k,a;cin>>n>>m>>k;for(int i=0;i<n;i++){cin>>a;mp[a]++;}for(int i=1;i<=m;i++){ve.pb(i);}sort(ve.begin(),ve.end(),cmp);//mp[ve[0]] n-kcout<<min(mp[ve[0]],n-k);
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
2.*
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e4+5;
const int inf=0x3f3f3f3f;
int n,m,k,a;
map<int,int>mp;
bool check(int mid,int i)
{int cn=0;for(auto x:mp){cn+=max(0ll,x.second-mid);}if(mp[i]>=mid)cn-=(mp[i]-mid);if(cn>k)return false;else return true;//k个名额够
}
void solve()
{cin>>n>>m>>k;for(int i=1;i<=n;i++){cin>>a;mp[a]++;}for(int i=1;i<=m;i++){if(n-k-mp[i]<0)cout<<-1<<' ';else{int l=0,r=n;while(l<r){int mid=(l+r)>>1;if(check(mid,i))r=mid;让mid小一点,cn大一点,直到临界else l=mid+1;}cout<<l<<' ';}}
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
3.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
const int inf=0x3f3f3f3f;
int a[N],b[N],c[N];
void solve()
{int n,m,u,cn1=0,cn2=0;cin>>n>>m>>u;for(int i=0;i<n;i++)cin>>a[i];for(int i=0;i<n;i++)cin>>b[i];for(int i=0;i<n;i++){cn1=0,cn2=0;for(int j=i;j<n;j++){cn1+=a[j],cn2+=b[j];if(cn1>m||cn2>u)break;else{c[i]++;}}}sort(c,c+n);cout<<max(0ll,c[n-1]);
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
4.*
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e4+5;
const int inf=0x3f3f3f3f;
int a[110],b[110];
bool cmp(int p,int q)
{return p>q;
}
void solve()
{int n,m,x,cn=0,sum=0;cin>>n>>m>>x;for(int i=0;i<n;i++)cin>>a[i];for(int i=0;i<m;i++)cin>>b[i];sort(a,a+n,cmp);sort(b,b+m,cmp);for(int i=0,j=0;i<n&&j<m;i++,j++){sum+=b[j];if(sum>=x){cn++;break;}else{sum-=b[j];sum+=a[i]*b[j];cn+=2;if(sum>=x){break;}}}if(sum<x)cout<<-1;else cout<<cn;
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
5.*
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
const int inf=0x3f3f3f3f;
void solve()
{int t;cin>>t;while(t--){int n,k,a[N],b[N]={-inf};cin>>n>>k;for(int i=1;i<=n;i++)cin>>a[i];int max1=-inf;bool flag=1;for(int i=1;i<=n;i++){b[i]=a[i]-k;//b>=a-kb[i]=max(b[i],b[i-1]);//b[i]>=b[i-1],同大取大if(b[i]>a[i]+k){cout<<"No"<<endl;flag=0;break;}}if(flag)cout<<"Yes"<<endl;}
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
倒计时69天-CSDN博客
3.*
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e4+5;
const int inf=0x3f3f3f3f;
int dp[10];
void solve()
{int n,mx=-inf;string s;cin>>n;for(int i=0;i<n;i++){cin>>s;int a=s[0]-'0',b=s.back()-'0';dp[b]=max(dp[b],dp[a]+1);mx=max(mx,dp[b]);}cout<<n-mx;
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}
倒计时69天-CSDN博客
1.
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
const int inf=0x3f3f3f3f;
int v[110],w[110],a[N],b[N],c[N];
void solve()
{int n,m,q,cn=0;cin>>n>>m>>q;for(int i=1;i<=q;i++){cin>>a[i];}for(int i=1;i<=q;i++){b[i]=a[i]-a[i-1]-1;}b[q+1]=n-a[q];for(int i=1;i<=m;i++){int k,s;cin>>k>>s;v[i]=pow(2,k);w[i]=s;}for(int l=1;l<=q+1;l++){int V=b[l];for(int i=1;i<=m;i++){for(int j=v[i];j<=V;j++)//审题仔细,“同一个健身计划可以多次完成,也能多刺获得健身增益”{c[j]=max(c[j],c[j-v[i]]+w[i]);}}cn+=c[V];}cout<<cn;
}
signed main()
{ios_base::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr);solve();return 0;
}