题解:并查集(模板)
#include <iostream>
#include<map>
using namespace std;
int father[2000006];
int rank1[1000005];
void init(int n){for(int i=1;i<=1e5;i++){father[i]=i;rank1[i]=1;}
}
int find(int x){if(father[x]==x){return x;}while(x!=father[x]){x=father[x];}return x;
}
void merge(int x,int y){int x1=find(x);int y1=find(y);if (x1 != y1) //如果不是同一个集合{if (rank1[x1] < rank1[y1]) //rank大的集合合并rank小的集合{father[x1]=y1;rank1[y1]++; }else if(rank1[x1] >= rank1[y1]){father[y1]=x1;rank1[x1]++;}}}
map<int,int>mp;
int main() {int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);init(n);for(int i=0;i<n;i++){int x,y;scanf("%d%d",&x,&y);merge(x,y);}int res=1;for(int i=1;i<=1e5;i++){int k=find(i);mp[k]++;int w=mp[k]; res=max(res,w);}printf("%d\n",res);mp.clear();}return 0;}
题解:首先对于字符串大小的比较需要重写,然后利用优先队列维护前K个字符在队列中,最终取出队首元素即可
#include <iostream>
#include<stdio.h>
#include<queue>
#include<map>
#include<vector>
#include<string>
using namespace std;bool cmp1(string& a, string& b){if(a.size()==b.size())
{if(a<b){return false;}else {return true;}
}else if(a.size()>b.size()){string c=a.substr(0,b.size());if(c==b){return false;}else if(c>b){return true;}else{return false;}}else {string c=b.substr(0,a.size());if(c==a) return true;else if(c>a){return false;}else{return true;}}
}struct cmp {bool operator() (const string& a, const string& b) const {if(a.size()==b.size()){if(a<b){return false;}else {return true;}}else if(a.size()>b.size()){string c=a.substr(0,b.size());if(c==b){return false;}else if(c>b){return true;}else{return false;}}else {string c=b.substr(0,a.size());if(c==a) return true;else if(c>a){return false;}else {return true;}}}
};
// priority_queue<string, greater<string>, cmp> que;
priority_queue<string, vector<string>,cmp> que;
map<string,int>mp;
int main() {string s;cin>>s;int k;scanf("%d",&k);// vector<string>ve(k,"");for(int i=0;i<s.size();i++){char c=s[i]-'a';s[i]=100-c;}int num=0;for(int i=0;i<s.size();i++){for(int j=i;j<s.size();j++){string s1=s.substr(i,j-i+1);// cout<<s1<<endl;if(num<k){if(mp[s1]){continue;}else{mp[s1]++;}que.push(s1);num++;}else{string s2=que.top();if(cmp1(s1,s2)){if(mp[s1]){continue;}else{mp[s1]++;}// cout<<(s1>s2)<<endl;// cout<<s1<<endl;// cout<<s2<<endl;que.pop();que.push(s1);}}}}// while(!que.empty()){// cout<<que.top()<<endl;// que.pop();// }// bool se="ddcc"<"ddc";// cout<<se;string res=que.top();for(int i=0;i<res.size();i++){res[i]=100+'a'-res[i];}cout<<res<<endl;// printf("%s\n",res);return 0;
}
// 64 位输出请用 printf("%lld")
题解:暴力求解
#include <iostream>
#include<queue>
using namespace std;
queue<int>que;
int main() {int T;scanf("%d",&T);while(T--){while(!que.empty()){que.pop();}int n;scanf("%d",&n);for(int i=0;i<n;i++){string s;cin>>s;getchar();if(s=="PUSH"){int x;scanf("%d",&x);que.push(x);}else if(s=="TOP"){if(que.empty()){printf("%d\n",-1);}else{printf("%d\n",que.front());}}else if(s=="POP"){if(que.empty()){printf("%d\n",-1);}else{que.pop();}}else if(s=="SIZE"){if(que.empty()){printf("%d\n",0);}else{printf("%d\n",que.size());}}else if(s=="CLEAR"){if(!que.empty()){while(!que.empty()){que.pop();}}}}}return 0;
}
// 64 位输出请用 printf("%lld")
题解:先判断是否有交点,如果有,分段使用积分
#include <iostream>
#include<stdio.h>
#include<math.h>
using namespace std;int main() {int T;scanf("%d",&T);while(T--){int A,B,C;scanf("%d%d%d",&A,&B,&C);int a=B*B;int b=2*B*C-2*A;int c=C*C;int t=b*b-4*a*c;if(t<0){double res=0;printf("%.10f\n",res);// printf("%.10f\n",0);出现了错误,由于printf存在缓冲区,会去取索引位0指针的值导致出错}else if(t==0){double x=-1.0*b/(2*a*1.0);double s=sqrt(2*A*1.0)*x*sqrt(x*1.0)*4/3.0;printf("%.10f\n",s);}else if(t>0){double x1=(-1*b-sqrt(t*1.0))/(2*a*1.0);double x2=(-1*b+sqrt(t*1.0))/(2*a*1.0);double s1=sqrt(2*A*1.0)*x1*sqrt(x1*1.0)*4/3.0;double s2=(sqrt(2*A*1.0)*x2*sqrt(x2*1.0)*2/3.0-B*x2*x2/2.0-C*x2)-(sqrt(2*A*1.0)*x1*sqrt(x1*1.0)*2/3.0-B*x1*x1/2.0-C*x1);if(C<=0){printf("%.10f\n",s1+s2);}else{printf("%.10f\n",s2);}}}return 0;
}