银行业务队列简单模拟
题解:
队列模拟,注意编号奇数到A,偶数B
Code:
#include <bits/stdc++.h>
using namespace std;
queue<int> q1,q2;
int main(void) {
ios::sync_with_stdio(0);
int n;
cin>>n;
for(int i=1;i<=n;i++) {
int x;
cin>>x;
if(x&1) q1.push(x);
else q2.push(x);
}
int time=1,i=0,j=0,flag=0;
while(q1.size()&&q2.size()) {
if(flag) cout<<" ";
cout<<q1.front();
flag=1;
q1.pop();
if(time%2==0) {
if(flag) cout<<" ";
cout<<q2.front(),q2.pop();
flag=1;
}
time++;
}
while(q1.size()) {
if(flag) cout<<" ";
cout<<q1.front(),q1.pop();
flag=1;
}
while(q2.size()) {
if(flag) cout<<" ";
cout<<q2.front(),q2.pop();
flag=1;
}
return 0;
}
月饼
题解:
按单价排序,然后顺着选
注意库存和售价都可能是小数
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e3+10;
struct node {
double sum,val;
double tmp;
bool operator<(const node &a) const {
return tmp>a.tmp;
}
} an[maxn];
int main(void) {
ll n,m;
scanf("%lld %lld",&n,&m);
for(int i=1;i<=n;i++) scanf("%lf",&an[i].sum);
for(int i=1;i<=n;i++) {
scanf("%lf",&an[i].val);
an[i].tmp=an[i].val/an[i].sum;
}
sort(an+1,an+n+1);
double ans=0.0,cnt=0;
for(int i=1;i<=n;i++) {
if(cnt+an[i].sum<=m) ans+=an[i].val,cnt+=an[i].sum;
else {
double k=m-cnt;
ans+=an[i].tmp*k;
break;
}
}
printf("%.2f\n",ans);
return 0;
}
家谱处理
题解:
按家族关系建树,判断查询,细节有点多。。
1.堂兄弟不是兄弟
2.侄子不是后代
3.叔伯不是祖先
然后就硬模
Code:
#include <bits/stdc++.h>
using namespace std;
const int maxn=110;
int cnt,flag;
int fa[maxn],lenth[maxn],vis[maxn];
vector<int> G[maxn];
map<string,int> mp;
void dfs(int u,int k) {
if(flag) return;
if(u==k) {
flag=1;
return;
}
for(int i=0;i<G[u].size();i++) dfs(G[u][i],k);
}
int main(void) {
int n,m;
cin>>n>>m;
getchar();
string s;
for(int i=1;i<=n;i++) {
getline(cin,s);
int len=s.length(),k=0,pos;
for(pos=0;pos<len;pos++) {
if(s[pos]==' ') k++;
else break;
}
string t=s.substr(pos);
mp[t]=i;
lenth[i]=k/2+1;
vis[lenth[i]]=i;
if(lenth[i]==1) continue;
G[vis[lenth[i]-1]].push_back(i);
fa[i]=vis[lenth[i]-1];
}
while(m--) {
getline(cin,s);
if(s.find(" is a child of ")!=s.npos) {
string xx=s.substr(0,s.find(" is a child of "));
string yy=s.substr(s.find(" is a child of ")+15);
int x=mp[xx],y=mp[yy];
if(fa[x]==y) cout<<"True\n";
else cout<<"False\n";
}
if(s.find(" is the parent of ")!=s.npos) {
string xx=s.substr(0,s.find(" is the parent of "));
string yy=s.substr(s.find(" is the parent of ")+18);
int x=mp[xx],y=mp[yy];
if(fa[y]==x) cout<<"True\n";
else cout<<"False\n";
}
if(s.find(" is a sibling of ")!=s.npos) {
string xx=s.substr(0,s.find(" is a sibling of "));
string yy=s.substr(s.find(" is a sibling of ")+17);
int x=mp[xx],y=mp[yy];
if(fa[x]==fa[y]) cout<<"True\n";
else cout<<"False\n";
}
if(s.find(" is a descendant of ")!=s.npos) {
string xx=s.substr(0,s.find(" is a descendant of "));
string yy=s.substr(s.find(" is a descendant of ")+20);
int x=mp[xx],y=mp[yy];
flag=0;
dfs(y,x);
if(flag) cout<<"True\n";
else cout<<"False\n";
}
if(s.find(" is an ancestor of ")!=s.npos) {
string xx=s.substr(0,s.find(" is an ancestor of "));
string yy=s.substr(s.find(" is an ancestor of ")+19);
int x=mp[xx],y=mp[yy];
flag=0;
dfs(x,y);
if(flag) cout<<"True\n";
else cout<<"False\n";
}
}
return 0;
}
畅通工程之最低成本建设问题
题解:
最小生成树板子题
Code:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e3+10,maxm=1e4+10;
int cnt;
struct Edge {
int from,to,dist;
bool operator<(const Edge &a) const {
return dist<a.dist;
}
} edge[maxm];
int pre[maxn];
void add_edge(int a,int b,int c) {
edge[++cnt]={a,b,c};
}
int find(int x) {
return x==pre[x]?x:pre[x]=find(pre[x]);
}
int main(void) {
ios::sync_with_stdio(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) pre[i]=i;
for(int i=1;i<=m;i++) {
int a,b,c;
cin>>a>>b>>c;
add_edge(a,b,c);
}
sort(edge+1,edge+cnt+1);
int ans=0,k=0;
for(int i=1;i<=cnt;i++) {
int x=edge[i].from,y=edge[i].to;
int rx=find(x),ry=find(y);
if(rx==ry) continue;
pre[rx]=ry;
ans+=edge[i].dist;
if(++k==n-1) break;
}
if(k<n-1) cout<<"Impossible";
else cout<<ans<<endl;
return 0;
}
球队“食物链”
题解:
按照胜场关系建图,注意图不是对称的,主客场的结果可能不一样。
因为要使字典序最小且找出的是一个环,所以从1开始dfs找出一个满足要求的环
剪枝:vis数组储存有没有将该点加入到环中,如果未加入的点都无法到达1就return
Code:
#include <bits/stdc++.h>
using namespace std;
const int maxn=30;
int n,flag;
int vis[maxn];
int mat[maxn][maxn];
int res[maxn];
void dfs(int x,int u) {
if(flag) return;
if(x==n+1) {
if(mat[u][1]) flag=1;
return;
}
int pos;
for(pos=2;pos<=n;pos++) {
if(!vis[pos]&&mat[pos][1]) break;
}
if(pos==n+1) return;
for(int i=1;i<=n;i++) {
if(!vis[i]&&mat[u][i]) {
vis[i]=1;
res[x]=i;
dfs(x+1,i);
if(flag) return;
vis[i]=0;
res[x]=0;
}
}
}
int main(void) {
ios::sync_with_stdio(0);
cin>>n;
string s;
for(int i=1;i<=n;i++) {
cin>>s;
int len=s.length();
for(int j=0;j<len;j++) {
if(s[j]=='W') mat[i][j+1]=1;
if(s[j]=='L') mat[j+1][i]=1;
}
}
res[1]=1,vis[1]=1;
dfs(2,1);
if(!flag) cout<<"No Solution";
else {
cout<<res[1];
for(int i=2;i<=n;i++) cout<<" "<<res[i];
}
return 0;
}