2021年度训练联盟热身训练赛第二场
题目链接:https://ac.nowcoder.com/acm/contest/12794
来源:牛客网
A Binarize It
#include<bits/stdc++.h>
using namespace std;
const int M=1e5+10,mod=1e9+7;
#define ll long long
double cx,cy,sx,sy,fx,fy;
int n;
int main() {
int cnt=0;
scanf("%d",&n);
while(n--) {
cnt++;
int a;
scanf("%d",&a);
int b=1;
for(int i=1;;i++){
b*=2;
if(b>=a) break;
}
printf("Input value: %d\n%d\n\n",a,b);
}
return 0;
}
B g2g c u l8r
用map容器将字符串与字符串一一对应,翻译句子时,map中存在的直接输出相应的map键值,不存在的原样输出字符串。
#include<bits/stdc++.h>
using namespace std;
const int M=1e5+10,mod=1e9+7;
#define ll long long
map<string,string>mp;
int n,m;
int p[M];
string s,ss;
int main() {
scanf("%d\n",&n);
for(int i=1; i<=n; i++) {
cin>>s;
getchar();
getline(cin,ss);
mp[s]=ss;
}
scanf("%d\n",&m);
for(int i=1; i<=m; i++) {
getline(cin,s);
int l=s.length();
int cnt=0;
for(int j=0; j<l; j++)
if(s[j]==' ') {
p[++cnt]=j;
}
p[++cnt]=l;
p[0]=-1;
for(int j=1; j<=cnt; j++) {
ss=s.substr(p[j-1]+1,p[j]-p[j-1]-1);
if(mp.count(ss)) cout<<mp[ss]<<' ';
else cout<<ss<<' ';
}
cout<<endl;
}
return 0;
}
C Tip to be Palindrome
先算出成本的20%作为至少要给的小费(有小数点要加一),再逐次对小费加一,判断是否形成回文,是的话退出并输出,否则一直寻找。
#include<bits/stdc++.h>
using namespace std;
const int M=1e5+10,mod=1e9+7;
#define ll long long
int t,n,a[M];
int main() {
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int ans=(n+4)/5;
for(int i=ans+n;;i++)
{
int p=i,cnt=0;
while(p){
a[++cnt]=p%10;
p/=10;
}
int f=0;
for(int j=1;j<=cnt/2;j++){
if(a[j]!=a[cnt-j+1]) f=1;
}
if(!f) {
printf("Input cost: %d\n%d %d\n\n",n,i-n,i);
break;
}
}
}
return 0;
}
D Soccer Standings
用map容器将队名对应编号(1~n),再用结构体存储相关数据,根据比赛情况获得相关数据,根据积分、进球差、进球数(优先顺序1、2、3)从大到小排序。
积分:赢一场+3,平局一场+1,输一场+0;
进球差:进球数-输球数;
进球数:每一场比赛进的球数之和;
输球数:每一场比赛输给敌方队伍的球数之和(敌方的进球)。
#include<bits/stdc++.h>
using namespace std;
const int M=1e5+10,mod=1e9+7;
#define ll long long
int t,n,m,a[M];
map<string,int>mp;
map<int,string>pm;
struct node{
int ss;
int s1,s2,s3;
int jq,sq;
int qc;
int id;
}r[M];
int cmp(node x,node y){
if(x.ss==y.ss){
if(x.qc==y.qc){
return x.jq>y.jq;
}
else return x.qc>y.qc;
}
return x.ss>y.ss;
}
int main() {
scanf("%d",&t);
int cnt=0;
while(t--){
cnt++;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
string s;
cin>>s;
mp[s]=i;
pm[i]=s;
}
memset(r,0,sizeof(r));
for(int i=1;i<=m;i++){
string a,b;
int x,y;
cin>>a>>x>>b>>y;
if(x<y){
r[mp[b]].s1++;
r[mp[a]].s2++;
}
else if(y<x){
r[mp[a]].s1++;
r[mp[b]].s2++;
}
else if(y==x){
r[mp[a]].s3++;
r[mp[b]].s3++;
}
r[mp[a]].jq+=x;
r[mp[a]].sq+=y;
r[mp[b]].jq+=y;
r[mp[b]].sq+=x;
}
for(int i=1;i<=n;i++){
r[i].ss=r[i].s1*3+r[i].s3;
r[i].qc=r[i].jq-r[i].sq;
r[i].id=i;
}
sort(r+1,r+1+n,cmp);
printf("Group %d:\n",cnt);
for(int i=1;i<=n;i++){
cout<<pm[r[i].id]<<' '<<r[i].ss<<' '<<r[i].s1<<' '<<r[i].s2<<' '<<r[i].s3;
cout<<' '<<r[i].jq<<' '<<r[i].sq<<' '<<endl;
}
cout<<endl;
}
return 0;
}
E NIH Budget
背包问题
每个病毒的4种层次作为一层循环加入原先的二层循环,形成三层循环。
#include<bits/stdc++.h>
using namespace std;
const int M=1e6+10,mod=1e9+7;
#define ll long long
int dp[M],a[20][10],b[20][10];
int main() {
int t;
scanf("%d",&t);
int cnt=0;
while(t--) {
cnt++;
int d,w;
memset(dp,0,sizeof(dp));
scanf("%d %d",&d,&w);
for(int i=1; i<=d; i++) {
for(int j=1; j<=4; j++)
scanf("%d %d",&a[i][j],&b[i][j]);
}
int ans=0;
for(int i=1; i<=d; i++) {
for(int j=w; j>=0; j--) {
for(int k=1; k<=4; k++) {
if(j>=a[i][k]) {
dp[j]=max(dp[j],dp[j-a[i][k]]+b[i][k]);
}
}
ans=max(ans,dp[j]);
}
}
printf("Budget #%d: Maximum of %d lives saved.\n\n",cnt,ans);
}
}
F Interstellar Love
用并查集查找联通块数量和带循环的联通块数量。
#include<bits/stdc++.h>
using namespace std;
const int M=1e5+10,mod=1e9+7;
#define ll long long
int fa[M],vis[M],f[M];
int find(int x) {
if(x==fa[x]) return x;
else return fa[x]=find(fa[x]);
}
int main() {
int t;
scanf("%d",&t);
int cnt=0;
while(t--) {
cnt++;
int n,m;
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++)
fa[i]=i,vis[i]=0,f[i]=0;
int ct1=0,ct2=0;
for(int i=1; i<=m; i++) {
int u,v;
scanf("%d %d",&u,&v);
vis[u]=1;
vis[v]=1;
int x=find(u),y=find(v);
if(x==y) {
f[x]=1;//标记是否带循环
} else {
fa[x]=y;
f[y]|=f[x];//合并联通块,将标记传递
}
}
for(int i=1; i<=n; i++) {
if(vis[i]&&fa[i]==i)
{
ct1++;
if(f[i]) ct2++;
}
}
printf("Night sky #%d: %d constellations, of which %d need to be fixed.\n\n",cnt,ct1,ct2);
}
}
G Plate Spinning
盘子速度每秒减少5单位,手从一个盘子到另一个盘子花费0.5秒。
n个盘子形成一个圆环,要保持n个盘一直旋转,手要移动一圈n次。
移动时间=n*0.5;转盘停下时间=盘速/5。
在盘子停下之前,手能转完一圈为盘子加速,就成功了。
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,cnt=0;
cin>>a;
while(a--){
cnt++;
int n,p;
cin>>n>>p;
double t=1.0*p/5,tt=n*0.5;
cout<<"Circus Act "<<cnt<<":"<<endl;
if(n==1) cout<<"Chester can do it!";
else{
if(t>=tt) cout<<"Chester can do it!";
else cout<<"Chester will fail!";
}
cout<<endl<<endl;
}
}
H The Eternal Quest for Caffeine
(未写,预留)
在这里插入代码片
I Pegasus Circle Shortcut
圆上两个点(起点、终点),圆内n个点,判断走圆弧路短,还是通过圆内n个点到终点路短(n个依次走:起点到1,1到2,2到3,…,n-1到n,n到终点)。
#include<bits/stdc++.h>
using namespace std;
const int M=1e5+10,mod=1e9+7;
#define ll long long
double cx,cy,sx,sy,fx,fy;
int n;
double x,y;
double dis(double x,double y,double xx,double yy){
return sqrt(abs(x-xx)*abs(x-xx)+abs(y-yy)*abs(y-yy));
}
int main() {
int cnt=0;
while(~scanf("%lf %lf %lf %lf %lf %lf",&cx,&cy,&sx,&sy,&fx,&fy)) {
if(!cx&&!cy&&!sx&&!sy&&!fx&&!fy) break;
cnt++;
scanf("%d",&n);
double ans=0,Ans=0;
double x=dis(sx,sy,fx,fy);
double r=dis(cx,cy,sx,sy);
double a=acos(1-x*x/(2*r*r));
Ans=a*r;
scanf("%lf %lf",&x,&y);
ans+=dis(sx,sy,x,y);
sx=x;sy=y;
for(int i=2; i<=n; i++)
{
scanf("%lf %lf",&x,&y);
ans+=dis(sx,sy,x,y);
sx=x;sy=y;
}
ans+=dis(sx,sy,fx,fy);
if(Ans<ans)printf("Case #%d: Stick to the Circle.\n\n",cnt);
else printf("Case #%d: Watch out for squirrels!\n\n",cnt);
}
return 0;
}
J Lowest Common Ancestor
将16进制转换成二进制字符串,从高位取x,y相等的值存到新的字符串里,再将该字符串转为16进制。
比如x=a020fac,y=a030ccf
二进制为x=1010000000100000111110101100
y=1010000000110000110011001111
相同为10100000001
转为16进制501。
然后进制转换用map容器实现。
#include<bits/stdc++.h>
using namespace std;
const int M=1e5+10,mod=1e9+7;
#define ll long long
int t,n,m;
string a[M];
map<char,string>mp;
map<string,char>pm;
void init() {
mp['0']="0000";mp['1']="0001";mp['2']="0010";mp['3']="0011";
mp['4']="0100";mp['5']="0101";mp['6']="0110";mp['7']="0111";
mp['8']="1000";mp['9']="1001";mp['a']="1010";mp['b']="1011";
mp['c']="1100";mp['d']="1101";mp['e']="1110";mp['f']="1111";
pm["0000"]='0';pm["0001"]='1';pm["0010"]='2';pm["0011"]='3';
pm["0100"]='4';pm["0101"]='5';pm["0110"]='6';pm["0111"]='7';
pm["1000"]='8';pm["1001"]='9';pm["1010"]='a';pm["1011"]='b';
pm["1100"]='c';pm["1101"]='d';pm["1110"]='e';pm["1111"]='f';
}
int main() {
scanf("%d",&t);
init();
int cnt=0;
while(t--) {
cnt++;
string x,y;
cin>>x>>y;
if(x==y) {
printf("Case #%d: ",cnt);
cout<<x<<endl<<endl;
continue;
}
int l1=x.length(),l2=y.length();
string a,b;
for(int i=0; i<l1; i++) {
if(i==0) {
string p;
for(int j=0; j<4; j++) {
if(mp[x[i]][j]!='0') {
p=mp[x[i]].substr(j);
break;
}
}
a+=p;
} else
a+=mp[x[i]];
}
for(int i=0; i<l2; i++) {
if(i==0) {
string p;
for(int j=0; j<4; j++) {
if(mp[y[i]][j]!='0') {
p=mp[y[i]].substr(j);
break;
}
}
b+=p;
} else
b+=mp[y[i]];
}
string c;
int la=a.length(),lb=b.length();
for(int i=0; i<=min(la,lb); i++) {
if(a[i]==b[i]) {
c+=a[i];
} else break;
}
int lc=c.length();
string ans;
int ct=0;
for(int i=lc-4; i>=0; i-=4) {
string d=c.substr(i,4);
a[++ct]=pm[d];
}
int k=lc%4;
if(k!=0)
{
string f;
for(int i=1; i<=4-k; i++)
f+='0';
f+=c.substr(0,k);
a[++ct]=pm[f];
}
for(int i=ct; i>=1; i--)
ans+=a[i];
printf("Case #%d: ",cnt);
cout<<ans<<endl<<endl;
}
return 0;
}