HNAU 27th Training Problem 第 27次训练 2013 /10/06 AMA.模拟 poj 2390 B.递推 hdu 2501 Tiling_easy version C.数学 hdu 2701 D.搜索 poj 2243 A*搜索 E.贪心 hdu 4544 湫湫系列故事——消灭兔子 F.图论 hdu 4547 CD操作(金山居 LCA算法) G.高精度 hdu 4541 Ten Googol H.动态规划 hdu 4035 maze 概率 |
hdu 4544 湫湫系列故事——消灭兔子(贪心)http://blog.csdn.net/sprintfwater/article/details/8744294
hdu 4541 Ten Googol http://blog.csdn.net/sprintfwater/article/details/8744301
hdu 4547 CD操作 http://blog.csdn.net/sprintfwater/article/details/8941888
A.模拟 (快速幂) poj 2390
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int main()
{
double rate;
int r,m,y;
while(~scanf("%d%d%d",&r,&m,&y))
{
rate=1.0+0.01*r;
double ans=1;
while(y)
{
if(y&1)
ans*=rate;
rate=rate*rate;
y=y>>1;
}
ans=m*ans;
//without rounding(不要四舍五入)取整
printf("%d\n",(int)ans);
}
return 0;
}
B.递推 hdu 2501
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define M 32
int a[M]={0,1,3};
void init()
{
//递推,如果只用1*2的铺的话是f[n]=f[n-1]+f[n-2] 还加上一个2*2的还需加上一个f[n-2]
for(int i=3;i<M;i++)
a[i]=a[i-1]+2*a[i-2];
}
int main()
{
int T,n;
cin>>T;
init();
while(T--)
{
scanf("%d",&n);
printf("%d\n",a[n]);
}
return 0;
}
D 搜索
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define M 9
struct knight{
int x,y,step;
bool ok()
{
return (x>=0&&x<8&&y>=0&&y<8)?1:0;
}
bool operator ==(const knight&a)
{
return (x==a.x&&y==a.y)?1:0;
}
}s,e;
int G[M][M];
bool vis[M][M];
int dir[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};
queue<knight> Q;
int BFS()
{
if(s==e)return 0;
while(!Q.empty())
Q.pop();
memset(vis,0,sizeof(vis));
vis[s.x][s.y]=1;
knight pre,now;
Q.push(s);
while(!Q.empty())
{
pre=Q.front();
Q.pop();
for(int i=0;i<8;i++)
{
now.x=pre.x+dir[i][0];
now.y=pre.y+dir[i][1];
if(now.ok()&&!vis[now.x][now.y])
{
now.step=pre.step+1;
if(now==e)
return now.step;
vis[now.x][now.y]=1;
Q.push(now);
}
}
}
return -1;
}
int main()
{
char sa[3],sb[3];
while(~scanf("%s%s",sa,sb))
{
s.x=sa[0]-'a';s.y=sa[1]-'1';
s.step=0;
e.x=sb[0]-'a';e.y=sb[1]-'1';
printf("To get from %s to %s takes %d knight moves.\n",sa,sb,BFS());
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define M 100002
#define LL long long
struct arrow{
int cost,kill;
bool operator <(const arrow &a)const{
return cost>a.cost;
}
}arr[M];
/**每次选择一只血多的兔子,在伤害大于血量的所有箭中,选择金币最少的*/
int b[M];
bool cmp(int a,int b)
{ //兔子的血从多到少排序
return a>b;
}
bool cmp1(arrow a,arrow b)
{ //箭的伤害从大到小排序
return a.kill>b.kill;
}
priority_queue<arrow> Q;
int main()
{
int n,m;
int i,j;
while(~scanf("%d%d",&n,&m))
{
while(!Q.empty())Q.pop();
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
sort(b,b+n,cmp);
for(i=0;i<m;i++)
scanf("%d",&arr[i].kill);
for(i=0;i<m;i++)
scanf("%d",&arr[i].cost);
sort(arr,arr+m,cmp1);
LL allcost=0;int flag=0;
for(i=0,j=0;i<n;i++)
{ //选择所有能杀死这只兔子的箭
while(j<m&&arr[j].kill>=b[i])
{
Q.push(arr[j]);
j++;
}
if(Q.empty())
{
flag=1;
break;
}
allcost+=Q.top().cost;
Q.pop();
}
if(flag)
printf("No\n");
else
printf("%I64d\n",allcost);
}
return 0;
}
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 1001
int fa[MAXN];
int ques[MAXN][MAXN];
int ind[MAXN];
bool vis[MAXN];
int cnt[MAXN];
vector<int>edge[MAXN];
int n,m;
int find_father(int k) {
if(fa[k] == k)return k;
else return fa[k] = find_father(fa[k]);
}
void tarjan(int x){
for (int i=1; i<=n; i++) {
if (vis[i] && ques[x][i])
cnt[find_father(i)] += ques[x][i];
}
fa[x] = x;
vis[x] = true;
for (int i=0; i<edge[x].size(); i++) {
tarjan(edge[x][i]);
fa[edge[x][i]] = x;
}
}
int main() {
while (~scanf("%d", &n)) {
for (int i=1; i<=n; i++) {
edge[i].clear();
}
memset(ques, 0, sizeof(ques));
memset(vis, false, sizeof(vis));
memset(cnt, 0, sizeof(cnt));
memset(ind, 0, sizeof(ind));
int a, b;
for (int i=0; i<n; i++) {
scanf("%d:(%d)", &a, &m);
for (int j=0; j<m; j++) {
scanf(" %d", &b);
edge[a].push_back(b);
ind[b]++;
}
}
scanf("%d", &m);
for (int i=0; i<m; i++) {
scanf(" (%d %d)", &a, &b);
ques[a][b]++;
ques[b][a]++;
}
for (int i=1; i<=n; i++)
if (!ind[i]) {
tarjan(i); break;
}
for (int i=1; i<=n; i++)
if (cnt[i]) printf("%d:%d\n", i, cnt[i]);
}
return 0;
}
/**LCA算法:首先用CD ..回退到最近公共祖先,再一次性到达目的地。*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
#define M 10002
int indegree[M];// 用来查找root
int fa[M],vis[M][M],query[M][M];
vector G[M];
map<string,int>m;
int n;//图中节点的个数
struct Allquery{
int a,b;
};
void init()
{
m.clear();
for(int i=1;i<M;i++)
G[i].clear();
}
int main()
{
int T,k,q;
cin>>T;
int a,b;
char str1[42],str2[42];
while(T--)
{
init();
scanf("%d%d",&k,&q);
n=0;
for(i=1;i<k;i++)
{
scanf("%s%s",str1,str2);
if(m[str1]==0)m[str1]=++n;
a=m[str1];
if(m[str2]==0)m[str2]=++n;
b=m[str2];
G[b].push_back(a);
}
}
return 0;
}