1001
#include<bits/stdc++.h>
using namespace std;
#define maxn 10010
int n;
int main()
{
priority_queue<int,vector<int>,greater<int> > que;
cin>>n;
while(n--)
{
int x;
cin>>x;
que.push(x);
}
int ans=0;
while(que.size()>1)
{
int a=que.top();
que.pop();
int b=que.top();
que.pop();
ans+=a+b;
que.push(a+b);
}
cout<<ans<<endl;
return 0;
}
1002
#include<bits/stdc++.h>
using namespace std;
#define maxn 110
int dp[maxn];
int n,m;
int w[maxn],val[maxn];
int main()
{
cin>>m>>n;
for(int i=0;i<n;i++)
{
cin>>w[i]>>val[i];
}
for(int i=0;i<n;i++)
{
for(int j=w[i];j<=m;j++)
{
dp[j]=max(dp[j],dp[j-w[i]]+val[i]);
}
}
cout<<"max="<<dp[m];
return 0;
}
1003 。。没写出来,忘记字典树要怎么构建了。。想投机取巧用两个for循环看看能不能过,结果不出意料的超时了。
1004
#include<bits/stdc++.h>
using namespace std;
#define maxn 50
char maze[maxn][maxn];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int d[maxn][maxn];
int sum=0;
int n,m;
struct node
{
int x;
int y;
};
bool check(int x,int y)
{
if(x>=0&&y>=0&&x<n&&y<m)
return true;
return false;
}
int bfs()
{
node now,next;
queue<node> que;
memset(d,-1,sizeof(d));
d[0][0]=0;
now.x=now.y=0;
maze[0][0]='#';
que.push(now);
while(!que.empty())
{
now=que.front();
que.pop();
for(int i=0;i<4;i++)
{
next.x=now.x+dx[i];
next.y=now.y+dy[i];
if(check(next.x,next.y)&&maze[next.x][next.y]=='.')
{
maze[next.x][next.y]='#';
d[next.x][next.y]=d[now.x][now.y]+1;
que.push(next);
}
}
}
return d[n-1][m-1]+1;
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>maze[i][j];
}
}
cout<<bfs()<<endl;
return 0;
}
1005
#include<bits/stdc++.h>
using namespace std;
#define maxn 200010
int p[maxn],a[maxn];
int n,m;
int d[maxn];
int find(int x)
{
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
void uni(int x,int y)
{
int a=find(x);
int b=find(y);
if(a!=b)
{
p[a]=b;
d[b]+=d[a];
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
p[i]=i;
d[i]=1;
}
while(m--)
{
char op[2];
scanf("%s",op);
if(op[0]=='M')
{
int a,b;
cin>>a>>b;
uni(a,b);
}
else
{
int x;
scanf("%d",&x);
cout<<d[find(x)]<<endl;
}
}
return 0;
}
1006
#include<bits/stdc++.h>
using namespace std;
#define INF 1e9
int n,x,y,d;
int main()
{
int res=INF;
int a,b;//b da a xiao
cin>>n>>x>>y;
if(x>y)
{
a=y;
b=x;
}
else
{
a=x;
b=y;
}
d=n/b;
for(int i=0;i<d;i++)
{
if((n-(i*b))%a==0)
{
res=min(res,a);
}
else
res=min(res,(n-(i*b))%a);
}
cout<<res;
return 0;
}
最终结果
