【HDOJ6343】Graph Theory Homework(贪心)

题意:

给定n个点,每个点有权值a[i],从A走到B的花费是下取整sqrt(a[i]-a[j]),求从1号点走到n号点的最小花费

1<=n,a[i]<=1e5

思路:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<string>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #include<vector>
11 using namespace std;
12 typedef long long ll;
13 typedef unsigned int uint;
14 typedef unsigned long long ull;
15 typedef pair<int,int> PII;
16 typedef vector<int> VI;
17 #define fi first
18 #define se second 
19 #define MP make_pair
20 
21 const int N=110000;
22 int a[N],n,cas;
23 int read()
24 { 
25    int v=0,f=1;
26    char c=getchar();
27    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
28    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
29    return v*f;
30 }
31 
32 
33 int main()
34 {
35  // freopen("1.in","r",stdin);
36   //freopen("1.out","w",stdout);
37   scanf("%d",&cas);
38   for(int i=1;i<=cas;i++)
39   {
40    scanf("%d",&n);
41    for(int j=1;j<=n;j++) scanf("%d",&a[j]);
42    int t=sqrt(abs(a[n]-a[1]));
43    printf("%d
",t);
44   }
45   return 0;
46 }
原文地址:https://www.cnblogs.com/myx12345/p/9406511.html