CH1701
题目:有n堆水果,每堆有aKg,问合成一堆最少要搬多种的水果
思路:优先队列,默认从大到小,直接push负值(硬核重载),然后每次都挑最小的和一块,直到队列里只剩一个
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
using namespace std;
priority_queue<int> h;
int main(){
int n;cin>>n;
int a;
for(int i=0;i<n;i++){
cin>>a;
h.push(-a);//偷懒不用重载了 (其实是不会)
}
int ans=0;
int l;
for(int i=1;i<n;i++){
l=ans;
ans-=h.top();
h.pop();
ans-=h.top();
h.pop();
h.push(l-ans);
//cout<<ans-l<<endl;
}
cout<<ans;
return 0;
}
关于stl栈:
定义一个栈 stack<int> st;
操作跟其他的基本一样
CH1801
题目:大概就是求最长能匹配的括号串长度
思路:将括号进栈,因为字符串的对称性,当匹配到左括号时,栈顶是对应右括号就满足条件
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
#include<stack>
using namespace std;
char s[1000005];
stack<char> st;
int find(char a, char b) {
if (a == '(' && b == ')') return 1;
if (a == '[' && b == ']') return 1;
if (a == '{' && b == '}') return 1;
return 0;
}
int main() {
cin>>s;
int n = strlen(s), cnt = 0, ans = 0;
for (int i = 0; i < n ; i++) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
st.push(s[i]);
} else {
if (!st.empty() &&find(st.top(), s[i])) {//匹配到了
cnt+= 2;
st.pop();
} else {
ans = max(ans, cnt);
cnt = 0;
while (!st.empty()) st.pop();//清空
}
}
}
cout<<ans;
return 0;
}
P1655外加一洛谷
题目:有一天他脑子抽了,从口袋里拿出了 NN 个不同的球,想把它们放到 MM 个相同的盒子里,并且要求每个盒子中至少要有一个球,他好奇有几种放法
思路:斯特林数板子
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
string s[101][101]= {};
//s(n,m)=s(n-1,m-1)+m*s(n-1,m);
string cheng(int a,string b){
int ans[1000]={};
memset(ans,0,1000);
int lb=b.size();
for(int i=0;i<lb;i++){
ans[lb-i-1]=b[i]-'0';
}
int w=0 ;
for(int i=0;i<lb;i++){
ans[i]=ans[i]*a+w;
w=ans[i]/10;
ans[i]%=10;
}
while(w){
ans[lb++]+=w%10;
w/=10;
}
string anss;
lb--;
while(lb>=0){
anss+=ans[lb--]+'0';
}
return anss;
}
string jia(string a,string b){
int la=a.size() ;
int lb=b.size() ;
int aa[1000]={};
int bb[1000]={};
for(int i=0;i<la;i++){
aa[la-i-1]=a[i]-'0';
}
for(int i=0;i<lb;i++){
bb[lb-1-i]=b[i]-'0';
}
int maxl=max(la,lb);
for(int i=0;i<maxl;i++){
aa[i]+=bb[i];
aa[i+1]+=aa[i]/10;
aa[i]%=10;
}
maxl+=(aa[maxl]==0? 0:1) ;
string ans;
for(int i=maxl-1;i>=0;i--){
ans+=aa[i]+'0';
}
return ans;
}
void ss(int n,int m){
string a=cheng(m,s[n-1][m]);
s[n][m]=jia(s[n-1][m-1],a);
}
int main() {
for(int i=1;i<101;i++){
s[i][1]='1';
}
for(int i=2;i<101;i++){
for(int j=1;j<=i;j++){
ss(i,j);
}
}
/*
for(int i=1;i<=10;i++){
for(int j=1;j<=i;j++){
cout<<s[i][j]<<" ";
}
cout<<endl;
}
*/
int n,m;
while(cin>>n>>m) {
if(m>n) {
cout<<0<<endl;
}
else if(m==n){
cout<<1<<endl;
}else {
cout<<s[n][m]<<endl;
}
}
return 0;
}
//s(n+1,m)= s(n,m-1)+m*s(n,m)
/* n个放m里
1
1 1
1 3 1
1 7 6 1
*/