https://pintia.cn/problem-sets/994805046380707840/problems/1111914599412858889
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
ll n,m,k,a[maxn],f,num,s;
int main(){
cin>>n>>m>>k;
while(k--){
for(int i=1;i<=n;i++){
cin>>a[i];
}
stack<ll> st;
while(!st.empty()){
st.pop();
}
ll top=1,cnt=0;f=0;num=n;s=1;//cnt容量 num个用了记录 s下标
while(num){
if(s>n)
break;
while((!st.empty() && top==st.top()) || a[s]==top){
if(!st.empty() && top==st.top()){
top++;
st.pop();
cnt--;
num--;
}
if(a[s]==top){
top++;s++;num--;
}
}
if(cnt<m){
if(s<=n){
st.push(a[s]);//while s++到n+1 压进去0
cnt++; s++;
}
}
else{ //超lim
f=1;
break;
}
}
// while(!st.empty()){
// cout<<st.top();
// st.pop();
//
// }
// cout<<endl;
if(!st.empty() || f ){ //5 6 7 1 2 3 4 栈不空 超lim
cout<<"NO"<<endl;
}
else
cout<<"YES"<<endl;
}
return 0;
}
https://pintia.cn/problem-sets/994805046380707840/problems/994805053695574016
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int n,m,k,a[maxn],num;
string s;
int main(){
vector<int> v1,v;//v1栈 v 从小到大
vector<int>::iterator it;
cin>>n;
while(n--){
cin>>s;
if(s=="Push"){
cin>>num;
it=lower_bound(v.begin(),v.end(),num);
v.insert(it,num);
v1.push_back(num);
}
else if(s=="Pop")
{
if(v1.size()==0)
cout<<"Invalid"<<endl;
else
{
it=lower_bound(v.begin(),v.end(),v1[v1.size()-1]);
v.erase(it);
cout<<v1[v1.size()-1]<<endl;
v1.pop_back();
}
}
else {
if(v1.size()==0){
cout<<"Invalid"<<endl;
continue;
}
else{
if(v.size()&1)
cout<<v[v.size()/2]<<endl;
else
cout<<v[v.size()/2-1]<<endl;
}
}
}
return 0;
}
并查集
#include<bits/stdc++.h>
using namespace std;
int n,k,x,pre[1005],course[1005],num[1005],cnt,c;//pre人 1000个爱好的根 每个爱好多少人
string s;
//兴趣爱好相同的人合并
bool cmp(int a,int b){
return a>b;
}
int find(int x){
return pre[x]==x?x:pre[x]=find(pre[x]);
}
void Union(int x,int y){
int fx=find(x),fy=find(y);
if(fx==fy)
return;
pre[fx]=fy;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
pre[i]=i;
for(int i=1;i<=n;i++){
scanf("%d:",&k);
for(int j=1;j<=k;j++){
cin>>x;
if(course[x]==0)
course[x]=i;//你就是此兴趣爱好的领头羊
Union(i,course[x]);//兴趣相同的人进行合并
}
}
for(int i=1;i<=n;i++){
int f=find(i);
if(num[f]==0) //这个部落暂时没有首领 第一次出现
cnt++;
num[f]++;//统计每个部落人数
}
sort(num+1,num+n+1,cmp);
cout<<cnt<<endl;
for(int i=1;num[i];i++){
if(c==0)
printf("%d",num[i]),c++;
else
printf(" %d",num[i]);
}
cout<<endl;
return 0;
}
dfs
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
int n,m,x,cnt,a[maxn],sum,f,c;
string s;
vector<int> ans;
//恰好相等也有多组解
void dfs(int i,int sum,vector<int> v){
if(i==n+1 || f==1 || sum>m)
return;
int cur=sum+a[i];
if(cur==m){
v.push_back(a[i]);
f=1;
ans=v;
return;
}
if(cur<m){
v.push_back(a[i]);
dfs(i+1,cur,v);
}
if(!v.empty()) v.pop_back();
dfs(i+1,sum,v);
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
sum+=a[i];
}
sort(a+1,a+n+1);
if(sum<m)
return cout<<"No Solution"<<endl,0;
vector<int> v;
dfs(1,0,v);
if(ans.size()==0){
cout<<"No Solution";
return 0;
}
for(int i=0; i<ans.size(); i++) {
printf("%d%c", ans[i], i==ans.size()-1?'\n':' ');
}
return 0;
}
谁先倒
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e4+5;
ll n,m,sum,f;
ll a,b,jiahan,jiahua,yihan,yihua,jia,yi;
int main(){
cin>>a>>b;
cin>>n;
for(int i=1;i<=n;i++){
cin>>jiahan>>jiahua>>yihan>>yihua;
if(jiahua==jiahan+yihan && jiahua!=yihua)
jia++;
if(yihua==jiahan+yihan && jiahua!=yihua)
yi++;
if(jia>a){
cout<<"A"<<endl<<yi<<endl;break;
}
if(yi>b){
cout<<"B"<<endl<<jia<<endl;break;
}
}
return 0;
}
先序遍历或镜像的先序遍历为二叉搜索树 输出YES和后根遍历顺序 否则输出NO
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
ll n,a[maxn],flag;//0:先序 1:镜像先序
vector<int> v;
void find(ll l,ll r){
if(l>r) return;//退出判断
ll tl=l+1,tr=r;
if(flag==0){
while(tl<=r && a[tl]<a[l]) tl++;
while(tr>l && a[tr]>=a[l]) tr--;
}
else{
while(tl<=r && a[tl]>=a[l]) tl++;
while(tr>l && a[tr]<a[l]) tr--;
}
if(tl-tr!=1) return;//tl=4 tr=3
find(l+1,tr);
find(tl,r);
v.push_back(a[l]);
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
find(0,n-1);
if(v.size()!=n){
flag=1;
v.clear();
find(0,n-1);
}
if(v.size()!=n)
cout<<"NO"<<endl;
else{
cout<<"YES"<<endl;
for(int i=0;i<v.size();i++){
printf("%d%c",v[i],i==v.size()-1?'\n':' ');//%d v[i]
}
}
return 0;
}
分数有正有负 long int型 ll可能不约分会爆 不断约分
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
ll n,suma,sumb=1,a,b,g,f;//0:先序 1:镜像先序
ll gcd(ll a,ll b){
return b==0?a:gcd(b,a%b);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
scanf("%lld/%lld",&a,&b);
g=(suma==0 || sumb==0)?1:gcd(abs(suma),abs(sumb));
suma/=g;sumb/=g;
g=(a==0 || b==0)?1:gcd(abs(a),abs(b));
a/=g;b/=g;
suma=suma*b+sumb*a;
sumb=sumb*b;
}
ll integer=suma/sumb;
suma=suma-sumb*integer;
g=(suma==0 || sumb==0)?1:gcd(abs(suma),abs(sumb));
suma/=g;sumb/=g;
if(integer!=0){
cout<<integer;
if(suma!=0)
cout<<" ";
}
if(suma!=0){
printf("%lld/%lld",suma,sumb);
}
if(integer==0 && suma==0)
cout<<0;
cout<<endl;
return 0;
}