百度之星专题

百度之星专题

HDU 4824

参考AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<bits/stdc++.h>
#define inf 0x7f7f7f7f
#define maxn 3005
using namespace std;
struct node{
int x,y;
}point[maxn];
int n;
int dp[maxn][maxn];
int dist(int i,int j){
int ans=point[i].x-point[j].x;
if(ans<0) ans=-ans;
int t=point[i].y-point[j].y;
if(t<0) t=-t;
if(360-t<t) t=360-t;
return ans*400+t;
}
int solve(){
dp[1][2]=dist(1,2);
for(int j=3;j<=n;j++){
for(int i=1;i<=j-2;i++){
dp[i][j]=dp[i][j-1]+dist(j-1,j);
}
dp[j-1][j]=inf;
for(int k=1;k<=j-2;k++){
int temp=dp[k][j-1]+dist(k,j);
if(temp<dp[j-1][j]) dp[j-1][j]=temp;
}
}
dp[n][n]=dp[n-1][n]+dist(n-1,n);
return dp[n][n]+(n*10)-10;
}
int main(){
// freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
point[1].x=0,point[1].y=0;
n++;
for(int i=2;i<=n;i++) scanf("%d%d",&point[i].x,&point[i].y);
printf("%d\n",solve());
}
return 0;
}

思路

双调欧几里德
转跳博客

HDU 4825

题目要求

给次给出一个询问 要求在数组中找出某个数与该数的xor值最大

参考AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int M = 55;
const int N = M*1e5;
struct Node {
ll val;
int l;
int r;
void clear() {
l = r = -1;
}
}node[N];
int p;
ll a, t[M];
void insert (int &root, int d, ll u) {
if (root == -1) {
root = p++;
node[root].clear();
}
if (d == -1) {
node[root].val = u;
return;
}
if (u & t[d]) insert(node[root].r, d - 1, u);
else insert(node[root].l, d - 1, u);
}
void query(int root, int d, ll u) {
if (d == -1) {
printf("%lld\n", node[root].val);
return;
}
if (((u & t[d]) && node[root].l != -1) || node[root].r == -1) query(node[root].l, d - 1, u);
else query(node[root].r, d - 1, u);
}
int main () {
// freopen("input.txt","r",stdin);
int cas, n, m;
scanf("%d", &cas);
t[0] = 1;
for (int i = 1; i < 55; i++)
t[i] = t[i-1] * 2;
for (int i = 1; i <= cas; i++) {
p = 0;
int root = -1;
scanf("%d%d", &n, &m);
for (int j = 0; j < n; j++) {
scanf("%lld", &a);
insert(root, 50, a);
}
printf("Case #%d:\n", i);
for (int j = 0; j < m; j++) {
scanf("%lld", &a);
query(0, 50, a);
}
}
return 0;
}

思路

字典树

HDU 4826

参考AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<bits/stdc++.h>
#define maxn 105
using namespace std;
int g[maxn][maxn];
int dp[maxn][maxn][3];
int flag=1;
int n,m;
int main(){
// freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&g[i][j]);
}
}
memset(dp,-0x3f3f3f3f,sizeof(dp));
dp[1][1][0]=dp[1][1][1]=dp[1][1][2]=g[1][1];
for(int i=2;i<=n;i++) dp[i][1][0]=dp[i-1][1][0]+g[i][1];
for(int j=2;j<=m;j++){
for(int i=1;i<=n;i++) dp[i][j][2]=max(max(dp[i][j-1][0],dp[i][j-1][1]),dp[i][j-1][2])+g[i][j];
for(int i=n-1;i>=1;i--) dp[i][j][1]=max(dp[i+1][j][1],dp[i+1][j][2])+g[i][j];
for(int i=2;i<=n;i++) dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][2])+g[i][j];
}
printf("Case #%d:\n%d\n",flag++,max(max(dp[1][m][0],dp[1][m][1]),dp[1][m][2]));
}
return 0;
}

思路

dfs超时
dp即可 第三维记录方向 初始化第一列 第二列开始更新每个方向的状态即可

文章目录
  1. 1. 百度之星专题
    1. 1.1. HDU 4824
      1. 1.1.1. 参考AC代码
      2. 1.1.2. 思路
    2. 1.2. HDU 4825
      1. 1.2.1. 题目要求
      2. 1.2.2. 参考AC代码
      3. 1.2.3. 思路
    3. 1.3. HDU 4826
      1. 1.3.1. 参考AC代码
      2. 1.3.2. 思路
|