2014编程之美初赛第一场题解

第一题:http://hihocoder.com/contest/msbop2014r2a/problem/1

直接打公式即可,可以用strtod和string减少工作量,选错gcc了,贡献了一个ce...........

  1 // #pragma comment(linker, "/STACK:102400000,102400000")
  2 #include <cstdio>
  3 #include <iostream>
  4 #include <cstring>
  5 #include <string>
  6 #include <cmath>
  7 #include <set>
  8 #include <list>
  9 #include <map>
 10 #include <iterator>
 11 #include <cstdlib>
 12 #include <vector>
 13 #include <queue>
 14 #include <stack>
 15 #include <algorithm>
 16 #include <functional>
 17 using namespace std;
 18 typedef long long LL;
 19 #define ROUND(x) round(x)
 20 #define FLOOR(x) floor(x)
 21 #define CEIL(x) ceil(x)
 22 const int maxn = 10010;
 23 const int maxm = 0;
 24 const int inf = 0x3f3f3f3f;
 25 const LL inf64 = 0x3f3f3f3f3f3f3f3fLL;
 26 const double INF = 1e30;
 27 const double eps = 1e-6;
 28 const int P[4] = {0, 0, -1, 1};
 29 const int Q[4] = {1, -1, 0, 0};
 30 const int PP[8] = { -1, -1, -1, 0, 0, 1, 1, 1};
 31 const int QQ[8] = { -1, 0, 1, -1, 1, -1, 0, 1};
 32 int kase;
 33 char str1[maxn], str2[maxn], str3[maxn];
 34 void init()
 35 {
 36     kase++;
 37 }
 38 void input()
 39 {
 40     scanf("%s%s%s", str1, str2, str3);
 41 }
 42 void debug()
 43 {
 44     //
 45 }
 46 double f1, f2, f3;
 47 void solve()
 48 {
 49     string tmp1, tmp2;
 50     int strn = strlen(str1);
 51     tmp1.clear();
 52     tmp2.clear();
 53     for (int i = 0; i < strn; i++)
 54     {
 55         if (str1[i] == '.' || (str1[i] >= '0' && str1[i] <= '9'))
 56         {
 57             tmp1.push_back(str1[i]);
 58         }
 59         else tmp2.push_back(str1[i]);
 60     }
 61     f1 = strtod(tmp1.c_str(), NULL);
 62     if (tmp2 == "m") f1 *= 1000.0;
 63     else if (tmp2 == "dm") f1 *= 100.0;
 64     else if (tmp2 == "cm") f1 *= 10.0;
 65     else if (tmp2 == "dm") f1 *= 100.0;
 66     else if (tmp2 == "um") f1 /= 1000.0;
 67     else if (tmp2 == "nm") f1 /= 1000.0 * 1000.0;
 68 
 69     strn = strlen(str2);
 70     tmp1.clear();
 71     tmp2.clear();
 72     for (int i = 0; i < strn; i++)
 73     {
 74         if (str2[i] == '.' || (str2[i] >= '0' && str2[i] <= '9'))
 75         {
 76             tmp1.push_back(str2[i]);
 77         }
 78         else tmp2.push_back(str2[i]);
 79     }
 80     f2 = strtod(tmp1.c_str(), NULL);
 81     if (tmp2 == "m") f2 *= 1000.0;
 82     else if (tmp2 == "dm") f2 *= 100.0;
 83     else if (tmp2 == "cm") f2 *= 10.0;
 84     else if (tmp2 == "dm") f2 *= 100.0;
 85     else if (tmp2 == "um") f2 /= 1000.0;
 86     else if (tmp2 == "nm") f2 /= 1000.0 * 1000.0;
 87 
 88     strn = strlen(str3);
 89     tmp1.clear();
 90     tmp2.clear();
 91     for (int i = 0; i < strn; i++)
 92     {
 93         if (str3[i] == '.' || (str3[i] >= '0' && str3[i] <= '9'))
 94         {
 95             tmp1.push_back(str3[i]);
 96         }
 97         else tmp2.push_back(str3[i]);
 98     }
 99     f3 = strtod(tmp1.c_str(), NULL);
100 
101     double ans = f3 * (f1 / f2);
102 
103     printf("Case %d: %.2fpx
", kase, ans);
104 }
105 void output()
106 {
107     //
108 }
109 int main()
110 {
111     // std::ios_base::sync_with_stdio(false);
112     // #ifndef ONLINE_JUDGE
113     // freopen("in.cpp", "r", stdin);
114     // #endif
115     int T;
116     scanf("%d", &T);
117     kase = 0;
118     while (T--)
119     {
120         init();
121         input();
122         solve();
123         output();
124     }
125     return 0;
126 }
View Code

第二题:http://hihocoder.com/contest/msbop2014r2a/problem/2

神奇地暴力过了。。。。。。。。听说正解是用树状数组

dfs,第一次求深度,然后每次操作从u点dfs一遍(记录其fa节点即可直接从u点dfs),可以加入剪枝(dep>r的剪掉),注意边加delta边模防溢出

注意算最终的结果的时候要边乘边模,否则会溢出,而且乘magic的时候用long long

  1 // #pragma comment(linker, "/STACK:102400000,102400000")
  2 #include <cstdio>
  3 #include <iostream>
  4 #include <cstring>
  5 #include <string>
  6 #include <cmath>
  7 #include <set>
  8 #include <list>
  9 #include <map>
 10 #include <iterator>
 11 #include <cstdlib>
 12 #include <vector>
 13 #include <queue>
 14 #include <stack>
 15 #include <algorithm>
 16 #include <functional>
 17 using namespace std;
 18 typedef long long LL;
 19 #define ROUND(x) round(x)
 20 #define FLOOR(x) floor(x)
 21 #define CEIL(x) ceil(x)
 22 const int maxn = 1e5 + 10;
 23 const int maxm = 3e5 + 10;
 24 const int inf = 0x3f3f3f3f;
 25 const LL inf64 = 0x3f3f3f3f3f3f3f3fLL;
 26 const double INF = 1e30;
 27 const double eps = 1e-6;
 28 const int MOD = 1000000007;
 29 const int MAGIC = 12347;
 30 const int P[4] = {0, 0, -1, 1};
 31 const int Q[4] = {1, -1, 0, 0};
 32 const int PP[8] = { -1, -1, -1, 0, 0, 1, 1, 1};
 33 const int QQ[8] = { -1, 0, 1, -1, 1, -1, 0, 1};
 34 struct Node
 35 {
 36     int dep;
 37     int val;
 38     Node(int _dep = 0, int _val = 0): dep(_dep), val(_val) {}
 39 } node[maxn];
 40 struct Edge
 41 {
 42     int u, v;
 43     int next;
 44 } edge[maxm];
 45 int head[maxn], en;
 46 int fa[maxn];
 47 int n;
 48 void addSubEdge(int u, int v)
 49 {
 50     edge[en].u = u;
 51     edge[en].v = v;
 52     edge[en].next = head[u];
 53     head[u] = en++;
 54 }
 55 void addEdge(int u, int v)
 56 {
 57     addSubEdge(u, v);
 58     addSubEdge(v, u);
 59 }
 60 int kase;
 61 void init()
 62 {
 63     memset(head, -1, sizeof(head));
 64     en = 0;
 65     kase++;
 66 }
 67 void dfs1(int u, int p)
 68 {
 69     if (u != 0) node[u].dep = node[p].dep + 1;
 70     for (int i = head[u]; i != -1; i = edge[i].next)
 71     {
 72         int v = edge[i].v;
 73         if (v == p) continue;
 74         dfs1(v, u);
 75     }
 76 }
 77 void input()
 78 {
 79     scanf("%d", &n);
 80     for (int i = 0; i < n; i++) node[i] = Node();
 81     node[0].dep = 1;
 82     fa[0] = 0;
 83     for (int i = 1; i < n; i++)
 84     {
 85         int tt;
 86         scanf("%d", &tt);
 87         tt--;
 88         addEdge(tt, i);
 89         fa[i] = tt;
 90     }
 91     dfs1(0, -1);
 92 }
 93 void debug()
 94 {
 95     //
 96 }
 97 void dfs(int u, int p, int l, int r, int w)
 98 {
 99     if (node[u].dep <= r && node[u].dep >= l)
100     {
101         node[u].val += w;
102         node[u].val %= MOD;
103     }
104     if (node[u].dep == r) return;
105     for (int i = head[u]; i != -1; i = edge[i].next)
106     {
107         int v = edge[i].v;
108         if (v == p) continue;
109         if (node[v].dep > r) continue;
110         dfs(v, u, l, r, w);
111     }
112 }
113 void solve()
114 {
115     int Q;
116     scanf("%d", &Q);
117     int u, l, r, w;
118     while (Q--)
119     {
120         scanf("%d%d%d%d", &u, &l, &r, &w);
121         u--;
122         dfs(u, fa[u], l, r, w);
123     }
124     int ans = 0;
125     for (int i = 0; i < n; i++)
126     {
127         // cout << node[i].val << endl;
128         LL tmp = (LL)ans * (LL)MAGIC;
129         // cout << "tmp:" << tmp << endl;
130         tmp %= (LL)MOD;
131         ans = (int)tmp;
132         ans %= MOD;
133         // cout << ans << endl;
134         ans += node[i].val;
135         ans %= MOD;
136         // cout << ans << endl;
137     }
138     printf("Case %d: %d
", kase, ans);
139 }
140 void output()
141 {
142     //
143 }
144 int main()
145 {
146     // std::ios_base::sync_with_stdio(false);
147     // #ifndef ONLINE_JUDGE
148     // freopen("in.cpp", "r", stdin);
149     // #endif
150 
151     int T;
152     scanf("%d", &T);
153     kase = 0;
154     while (T--)
155     {
156         init();
157         input();
158         solve();
159         output();
160     }
161     return 0;
162 }
View Code

第三题:http://hihocoder.com/contest/msbop2014r2a/problem/3

三分法,显然要求的点x到其他所有点的距离之和随x轴先减后增,符合三分法

 1 // #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <string>
 6 #include <cmath>
 7 #include <set>
 8 #include <list>
 9 #include <map>
10 #include <iterator>
11 #include <cstdlib>
12 #include <vector>
13 #include <queue>
14 #include <stack>
15 #include <algorithm>
16 #include <functional>
17 using namespace std;
18 typedef long long LL;
19 #define ROUND(x) round(x)
20 #define FLOOR(x) floor(x)
21 #define CEIL(x) ceil(x)
22 const int maxn = 1e5 + 10;
23 const int maxm = 0;
24 const int inf = 0x3f3f3f3f;
25 const LL inf64 = 0x3f3f3f3f3f3f3f3fLL;
26 const double INF = 1e30;
27 const double eps = 1e-10;
28 const int P[4] = {0, 0, -1, 1};
29 const int Q[4] = {1, -1, 0, 0};
30 const int PP[8] = { -1, -1, -1, 0, 0, 1, 1, 1};
31 const int QQ[8] = { -1, 0, 1, -1, 1, -1, 0, 1};
32 int kase;
33 int xx[maxn], yy[maxn];
34 int n;
35 void init()
36 {
37     kase++;
38 }
39 void input()
40 {
41     scanf("%d", &n);
42     for (int i = 0; i < n; i++)
43         scanf("%d%d", &xx[i], &yy[i]);
44 }
45 void debug()
46 {
47     //
48 }
49 double dis(double ax, double ay, double bx, double by)
50 {
51     return sqrt((ax - bx) * (ax - bx) + (by - ay) * (by - ay));
52 }
53 double cal(double nx)
54 {
55     double ans = 0;
56     for (int i = 0; i < n; i++)
57         ans += dis(nx, 0, xx[i], yy[i]);
58     return ans;
59 }
60 void solve()
61 {
62     double Left, Right;
63     double mid, midmid;
64     double mid_area, midmid_area;
65     Left = 0;
66     Right = 1e6;
67     while (Left + eps < Right)
68     {
69         mid = (Left + Right) / 2;
70         midmid = (mid + Right) / 2;
71         mid_area = cal(mid);
72         midmid_area = cal(midmid);
73         if (mid_area < midmid_area) Right = midmid;
74         else Left = mid;
75     }
76     printf("Case %d: %.8f
", kase, Left);
77 }
78 void output()
79 {
80     //
81 }
82 int main()
83 {
84     // std::ios_base::sync_with_stdio(false);
85     // #ifndef ONLINE_JUDGE
86     // freopen("in.cpp", "r", stdin);
87     // #endif
88     int T;
89     scanf("%d", &T);
90     kase = 0;
91     while (T--)
92     {
93         init();
94         input();
95         solve();
96         output();
97     }
98     return 0;
99 }
View Code
原文地址:https://www.cnblogs.com/xysmlx/p/3675221.html