文章目录
试题链接
A - Johnny and Ancient Computer CodeForces - 1362A
题目详情
解题思路
签到题(看不懂题除外= =这道题题意我也是蒙的)题目大致意思就是计算前者经过几次给定规则的变换后能够变成后者,其中除法只有在能够被整除的情况下才能使用,若无法变为后者则输出-1,否则输出变换次数。
解题代码
Java:
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int t;
long a,b;
Scanner scanner = new Scanner(new BufferedInputStream(System.in));
t = scanner.nextInt();
while(t-- > 0){
a = scanner.nextLong();
b = scanner.nextLong();
int cont = 0;
boolean bool=true;
if(a<b){
while(true){
if(a == b) {
break;
}
if(a*8 <= b){
a*=8;
cont++;
}
else if(a*4 <= b){
a*=4;
cont++;
}
else if(a*2 <= b){
a*=2;
cont++;
}else {
System.out.println("-1");
bool=false;
break;
}
}
}else if(a>b){
while(true){
if(a==b){
break;
}
if(a%8==0 && a/8>=b){
a/=8;
cont++;
}else if(a%4==0 && a/4 >=b){
a/=4;
cont++;
}else if(a%2==0 && a/2 >= b){
a/=2;
cont++;
}else{
System.out.println("-1");
bool = false;
break;
}
}
}
if(bool){
System.out.println(cont);
}
}
}
}
技巧
百度的大佬思路:题目的意思就是给你一个a ,你可以对它乘2,乘4,乘8,或者对它除2,除4,除8,问你是否可以将a转化成b,如果可以输出最少的操作数,否者输出-1
所以我们就直接那小的那个数去一直x2,看最后能不能得到另外一个数,
因为x8相当于三次x2的操作,两次x4的操作,如果可以得到那我们就可以求出最少的操作数。
C++代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--) {
long long a,b;
cin>>a>>b;
if(a>b)
swap(a,b);
int cnt = 0 ;
while(a<b) {
a = a*2;
cnt++;
}
if(a!=b)
cout<<"-1"<<endl;
else
cout << cnt/3 + min(cnt%3, 1) << endl;;
}
return 0;
}
B - Garland CodeForces - 767C
题目详情
解题思路
大致题意
一棵树;
树上的每个节点都有一个权值;
让你把一棵树切掉两条边;
然后把这棵树分成了3个部分;
要求这3个部分,每个部分的权值和相同;
即sum1=sum2=sum3
题解
百度的题解思路:
树形DP;
一开始累加所有节点的权值和sum;
如果不是3的倍数则直接输出无解;
用cnt[x]记录x节点下方的子树和(权值和);
假设dfs到了第x个节点
考虑两种情况;
①
有两个节点y,z;
且sum/3==cnt[y]==cnt[z]
且y和z分别在x的两个儿子节点所在的子树中;
输出y和z就好
②
2/3*sum==cnt[x];
然后x的子树里面有另外一个节点y
满足1/3*sum==cnt[y];
输出x和z就好
这里注意x不能为根节点!
定义个数组往上传这个子树里面有没有1/3sum和2/3sum就好;
解题代码
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e6+1000;
int n,sum=0,goal;
int fa,w[N],root,cnt[N],haved1[N],haved2[N];
vector <int> G[N];
void dfs(int x)
{
cnt[x] = w[x];
int num = 0,a[5];
for (int y : G[x])
{
dfs(y);
if (haved1[y])
{
haved1[x] = haved1[y];
a[++num] = haved1[y];
if (num > 1)
{
printf("%d %d\n", a[1], a[2]);
exit(0);
}
}
if (haved2[y]) haved2[x] = haved2[y];
cnt[x] += cnt[y];
}
if (cnt[x] == goal) haved1[x] = x;
if (cnt[x] == 2 * goal) haved2[x] = x;
if (x!=root && 2*goal==cnt[x])
{
for (int y : G[x])
{
if (haved1[y])
{
printf("%d %d\n", haved1[y], x);
exit(0);
}
}
}
}
int main()
{
rei(n);
rep1(i, 1, n)
{
rei(fa), rei(w[i]);
if (fa == 0)
root = i;
G[fa].ps(i);
sum += w[i];
}
if (sum % 3) return puts("-1"), 0;
goal = sum / 3;
dfs(root);
puts("-1");
return 0;
}
C - 哈密顿绕行世界问题 HDU - 2181
题目详情
解题思路
DFS算法题,输入的20行分别是每个从城市和哪3个城市相通。(第一行是第一个城市和2,5,20三个城市相通,第二行是第二个城市和1,3,12三个城市相同,那么我们用一个二维数组存储,a[m][i]中m代表第几个城市i代表第m个城市和哪三个城市相通。从m城市出发,回到m城市时结束。
解题代码
#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"string"
using namespace std;
int sc[22][4],b[22],s[22];
int a,c;
int m;
void dfs(int y,int e)
{
if(e>=20&&y==m)//e等于20,并且回到m城的时候证明有一条路线并输出
{
cout<<a++<<": "<<m;
for(int i=0;i<20;i++)
{
cout<<' '<<s[i];
}
cout<<endl;
return ;
}
for(int i=0;i<3;i++)//每次查询当前城市能到达哪3个城市
{
if(b[sc[y][i]]==0)//如果当前城市没走过就走,走过就查询下一个
{
b[sc[y][i]]=1;//当前城市没走过就走并记录
s[e]=sc[y][i];//路线存到数组中
dfs(sc[y][i],e+1);//走下一个城市
b[sc[y][i]]=0;//记录清零,下一条路线时使用
}
}
return ;
}
int main()
{
for(int i=1;i<=20;i++)
{
cin>>sc[i][0]>>sc[i][1]>>sc[i][2];//把每个城市和哪三个城市相通存到数组中。
}
while(cin>>m)//每次要查询的城市
{
if(m==0)
{
break;
}
a=1,c=0;
memset(b,0,sizeof(b));
dfs(m,c);//m是从哪个城市开始,c是走了多少次。
}
return 0;
}
D - 敌兵布阵 HDU - 1166
题目详情
解题思路
这道题数据比较大,直接计算会超时。查了下好像要用啥线段树或者树状数组,一些资料:
线段树:线段树总结
树状数组:树状数组入门
解题代码
线段树:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct
{
int a,b,sum;
}t[140000];
int r[50010],summ,k;
void buildd(int x,int y,int num)//构造线段树
{
t[num].a=x;
t[num].b=y;
if(x==y)
t[num].sum=r[y];
else
{
buildd(x,(x+y)/2,num*2);
buildd((x+y)/2+1,y,num*2+1);
t[num].sum=t[num*2].sum+t[num*2+1].sum;
}
}
void query(int x, int y, int num)//查找
{
if(x<=t[num].a&&y>=t[num].b)
summ+=t[num].sum;
else{
int minn=(t[num].a+t[num].b)/2;
if(x>minn) query(x,y,num*2+1);
else if(y<=minn)
query(x,y,num*2);
else
{
query(x,y,num*2);
query(x,y,num*2+1);
}
}
}
void add(int x, int y, int num)//添加
{
t[num].sum+=y;
if(t[num].a==x&&t[num].b==x) return ;
if (x>(t[num].a+t[num].b)/2) add(x,y,num*2+1);
else add(x,y,num*2);
}
void sub(int x,int y,int num)//减少
{
t[num].sum-=y;
if(t[num].a==x&&t[num].b==x) return ;
if(x>(t[num].a+t[num].b)/2) sub(x,y,num*2+1);
else sub(x,y,num*2);
}
int main()
{
int t;
int cnt=0;
scanf("%d",&t);
while(t--)
{
int n,m;
char ch[10];
scanf("%d",&k);
r[0]=0;
for(int i=1;i<=k;i++)
scanf("%d",&r[i]);
buildd(1,k,1);
printf("Case %d:\n",++cnt);
while(scanf("%s",&ch))
{
if(strcmp(ch,"End")==0)
break;
else if(strcmp(ch,"Query")==0)
{
scanf("%d%d",&n,&m);
summ=0;
query(n,m,1);
printf("%d\n",summ);
}
else if(strcmp(ch,"Add")==0)
{
scanf("%d%d",&n,&m);
add(n,m,1);
}
else if(strcmp(ch,"Sub")==0)
{
scanf("%d%d",&n,&m);
sub(n,m,1);
}
}
}
return 0;
}
树状数组:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int a[500050];
int summ[500050];
int low(int x)//lowbit 函数,树状数组的核心
{
return x&(-x);
}
void build(int n)//建立树状数组
{
for(int i=1;i<=n;i++)
{
for(int j=i;j>=i-low(i)+1;j--)
summ[i]+=a[j];
}
}
int s_sum(int n)//求n前的和
{
int sum=0;
for(int i=n;i>0;i-=low(i))
sum+=summ[i];
return sum;
}
void update(int x,int y,int n)//对树状数组进行修改
{
for(int i=x;i<=n;i+=low(i))
summ[i]+=y;
}
int main()
{
int t,k,num=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&k);
memset(a,0,sizeof(a));
memset(summ,0,sizeof(summ));
for(int i=1;i<=k;i++)
scanf("%d",&a[i]);
build(k);
char x[10];
int a,b;
printf("Case %d:\n",num++);
while(scanf("%s",x))
{
if(strcmp(x,"End")==0)
break;
scanf("%d%d",&a,&b);
if(strcmp(x,"Query")==0)
printf("%d\n",s_sum(b)-s_sum(a-1));
else if(strcmp(x,"Add")==0)
update(a,b,k);
else if(strcmp(x,"Sub")==0)
update(a,-1*b,k);
}
}
return 0;
}
E - Dungeon Master POJ - 2251
题目详情
中文释义
Descriptions:
你被困在一个3D地牢中且继续寻找最短路径逃生!地牢由立方体单位构成,立方体单位中有的会充满岩石。向上下前后左右移动一个单位需要一分钟。你不能向对角线的四个方向移动且迷宫四周环绕着许多岩石。
是否可以逃出地牢?如果可以,则需要多少时间?
Input
输入的第一行包含一个数,表示地牢的数量。
每个地牢的描述,其第一行包含三个数L,R和C(均小于等于30)。
L表示地牢的层数;R和C分别表示每层地牢的行与列的大小。
随后输入地牢的层数L,每层中包含R行,每行中包含C个字符。
每个字符表示地牢的一个单元。’#‘表示岩石单元,’.‘表示空白单元。你的起始位置在点’S’,出口为’E’。
每层地牢的输入后都有一个空行。当L,R和C均为0时,输入结束。
Output
每个迷宫对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。
如果无法逃生,则输出如下
Trapped!
解题思路
三维最短路BFS,和二维的基本一样,就是原来4个方向,现在6个方向,原来数组是二维,现在是三维
解题代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int sl,sx,sy;
int el,ex,ey;
char mp[35][35][35];//记录地图
int vis[35][35][35];//标记是否走过
int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };//六个方向
int l,r,c;
struct node
{
int f,x,y;//位置
int step;//步数
friend bool operator<(node a,node b)//步数小的现出来,即时间少
{
return a.step>b.step;//优先队列,步数小的先访问
}
};
priority_queue<node>q;
/*************************bfs***************************/
void bfs()
{
node p;
p.f=sl;//初始化
p.x=sx;
p.y=sy;
p.step=0;
vis[sl][sx][sy]=1;
q.push(p);
while(!q.empty())
{
node s=q.top();
q.pop();
if(s.f==el&&s.x==ex&&s.y==ey)//满足条件
{
printf("Escaped in %d minute(s).\n",s.step);
return;
}
for(int i=0; i<6; i++)//6种走法
{
int tl=s.f+base[i][0];
int tx=s.x+base[i][1];
int ty=s.y+base[i][2];
if(mp[tl][tx][ty]!='#'&&tl>=0&&tl<l&&tx>=0&&tx<r&&ty>=0&&ty<c&&!vis[tl][tx][ty])//判断是否能走
{
node e;
e.f=tl;
e.x=tx;
e.y=ty;
e.step=s.step+1;
vis[e.f][e.x][e.y]=1;
q.push(e);
}
}
}
cout<<"Trapped!"<<endl;
}
/**********************************主函数*********************************/
int main()
{
while(cin >> l >> r >>c,l+r+c)
{
for(int i=0; i<l; i++)
{
for(int j=0; j<r; j++)
{
cin >> mp[i][j];
for(int k=0; k<c; k++)
{
if(mp[i][j][k]=='S')
{
sl=i;
sx=j;
sy=k;
}
if(mp[i][j][k]=='E')
{
el=i;
ex=j;
ey=k;
}
}
}
}
memset(vis,0,sizeof(vis));//每次都要初始化
bfs();
}
}