标签:
由于博主水平及智商不足,所以暂时只能放出下列的题目的题解。
A B D F G H
需要其他题解请自行前往 http://talk.icpc-camp.org/
题目地址:hihocoder 1227-1236
A. The Cats‘ Feeding Spots
题意:给出M个点,求以其中一个点为圆心,最小半径的圆,使得这个圆里恰好有N个点。
(半径一定要是整数,且点不能恰好在圆上)。
方法:暴力,稍微注意一下精度什么的就好了,1A。
1 #include <bits/stdc++.h> 2 #include <ext/hash_map> 3 #include <ext/hash_set> 4 #include <ext/pb_ds/assoc_container.hpp> 5 #include <ext/pb_ds/tree_policy.hpp> 6 #include <ext/pb_ds/priority_queue.hpp> 7 using namespace std; 8 using namespace __gnu_cxx; 9 using namespace __gnu_pbds; 10 #define XINF INT_MAX 11 #define INF 0x3F3F3F3F 12 #define MP(X,Y) make_pair(X,Y) 13 #define PB(X) push_back(X) 14 #define REP(X,N) for(int X=0;X<N;X++) 15 #define REP2(X,L,R) for(int X=L;X<=R;X++) 16 #define DEP(X,R,L) for(int X=R;X>=L;X--) 17 #define CLR(A,X) memset(A,X,sizeof(A)) 18 #define IT iterator 19 #define RIT reverse_iterator 20 typedef long long ll; 21 typedef unsigned long long ull; 22 typedef pair<int,int> PII; 23 typedef vector<PII> VII; 24 typedef vector<int> VI; 25 typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree_set; 26 typedef tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree; 27 #define PQ std::priority_queue 28 #define HEAP __gnu_pbds::priority_queue 29 #define X first 30 #define Y second 31 #define lson(X) ((X)<<1) 32 #define rson(X) ((X)<<1|1) 33 34 double x[100]; 35 double y[100]; 36 37 double dist(int i, int j) { 38 return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); 39 } 40 41 int main() 42 { 43 #ifdef LOCAL 44 freopen("in.txt","r",stdin); 45 #endif // LOCAL 46 int t; 47 scanf("%d",&t); 48 while(t--) { 49 int n,k; 50 scanf("%d%d",&n,&k); 51 REP(i,n) { 52 scanf("%lf%lf", &x[i], &y[i]); 53 } 54 if(k>n) { 55 puts("-1"); 56 continue; 57 } 58 int mx = INF; 59 REP(i,n) { 60 vector<double> v; 61 REP(j,n) 62 v.PB(dist(i,j)); 63 sort(v.begin(), v.end()); 64 65 double d = v[k-1]; 66 int r = (d + 1e-8 + 1); 67 int ok = 1, cnt = 0; 68 REP(j,n) { 69 if(fabs(v[j] - r) < 1e-8) ok = 0; 70 if(v[j] < r) cnt++; 71 } 72 if(ok && cnt == k) { 73 mx = min(mx, r); 74 } 75 } 76 printf("%d\n", mx==INF?-1:mx); 77 } 78 return 0; 79 }
B. Mission Impossible 6题意:典型的文本编辑模拟题。题意太长就不描述了。
解法:纯模拟,需要注意的是该题有一个最大长度限制m,如果有操作使得长度超过m则忽视该操作。复制粘贴操作、以及覆盖模式的时候需要注意一些细节。
比赛的时候以为直接模拟会T,于是用链表做了,1A。
然而赛后发现其他人用string暴力都过了,后来算了一下的确复杂度不会高(智商捉急)。
1 #include <bits/stdc++.h> 2 #include <ext/hash_map> 3 #include <ext/hash_set> 4 #include <ext/pb_ds/assoc_container.hpp> 5 #include <ext/pb_ds/tree_policy.hpp> 6 #include <ext/pb_ds/priority_queue.hpp> 7 using namespace std; 8 using namespace __gnu_cxx; 9 using namespace __gnu_pbds; 10 #define XINF INT_MAX 11 #define INF 0x3F3F3F3F 12 #define MP(X,Y) make_pair(X,Y) 13 #define PB(X) push_back(X) 14 #define REP(X,N) for(int X=0;X<N;X++) 15 #define REP2(X,L,R) for(int X=L;X<=R;X++) 16 #define DEP(X,R,L) for(int X=R;X>=L;X--) 17 #define CLR(A,X) memset(A,X,sizeof(A)) 18 #define IT iterator 19 #define RIT reverse_iterator 20 typedef long long ll; 21 typedef unsigned long long ull; 22 typedef pair<int,int> PII; 23 typedef vector<PII> VII; 24 typedef vector<int> VI; 25 typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree_set; 26 typedef tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree; 27 #define PQ std::priority_queue 28 #define HEAP __gnu_pbds::priority_queue 29 #define X first 30 #define Y second 31 #define lson(X) ((X)<<1) 32 #define rson(X) ((X)<<1|1) 33 34 char s[10010]; 35 36 int S = 0; 37 int tot; 38 39 int L[100100], R[100100]; 40 char c[100100]; 41 42 #define INSERT_MODE 0 43 #define OVERWITE_MODE 1 44 45 char clip[10010]; 46 47 int main() 48 { 49 #ifdef LOCAL 50 freopen("in.txt","r",stdin); 51 #endif // LOCAL 52 int t; 53 scanf("%d",&t); 54 while(t--) { 55 int m; 56 scanf("%d%s", &m, s); 57 L[S] = NULL; 58 R[S] = NULL; 59 c[S] = ‘-‘; 60 tot = 1; 61 int pos = S; 62 int mode = INSERT_MODE; 63 int start = -1; 64 int length = 0; 65 int left = 0; 66 int sel_length = 0; 67 int clip_length = 0; 68 for(int i = 0; s[i]; i++) { 69 if(s[i] == ‘L‘) { 70 if(pos != S) { 71 pos = L[pos]; 72 left--; 73 if(start != -1) sel_length--; 74 } 75 }else if(s[i] == ‘R‘){ 76 if(R[pos] != NULL) { 77 pos = R[pos]; 78 left++; 79 if(start != -1) sel_length++; 80 } 81 }else if(s[i] == ‘S‘){ 82 mode = 1 - mode; 83 }else if(s[i] == ‘D‘){ 84 if(start == -1) { 85 if(R[pos] != NULL) { 86 L[R[pos] = R[R[pos]]] = pos; 87 length--; 88 } 89 }else{ 90 if(pos != start) { 91 if(sel_length < 0) { 92 L[R[pos] = R[start]] = pos; 93 }else{ 94 L[R[start] = R[pos]] = start; 95 } 96 length -= abs(sel_length); 97 } 98 start = -1; 99 } 100 }else if(s[i] == ‘B‘){ 101 if(pos != S) { 102 left--; 103 pos = L[pos]; 104 L[R[pos] = R[R[pos]]] = pos; 105 length--; 106 } 107 }else if(s[i] == ‘C‘){ 108 if(start == -1) { 109 start = pos; 110 sel_length = 0; 111 }else{ 112 clip_length = 0; 113 int st = start, ed = pos; 114 if(sel_length < 0) swap(st, ed); 115 for(int j = st; j != ed; j = R[j]) { 116 clip[clip_length++] = c[R[j]]; 117 } 118 start = -1; 119 } 120 }else if(s[i] == ‘V‘){ 121 if(mode == INSERT_MODE) { 122 if(clip_length + length <= m) { 123 REP(j,clip_length) { 124 L[R[tot] = R[pos]] = tot; 125 L[R[pos] = tot] = pos; 126 c[tot] = clip[j]; 127 pos = tot++; 128 left++; 129 length++; 130 } 131 } 132 }else if(mode == OVERWITE_MODE) { 133 if(clip_length + left <= m) { 134 REP(j,clip_length) { 135 if(R[pos] != NULL) { 136 L[R[pos] = R[R[pos]]] = pos; 137 length--; 138 } 139 L[R[tot] = R[pos]] = tot; 140 L[R[pos] = tot] = pos; 141 c[tot] = clip[j]; 142 pos = tot++; 143 left++; 144 length++; 145 } 146 } 147 } 148 }else{ 149 if(mode == INSERT_MODE && 1 + length <= m) { 150 L[R[tot] = R[pos]] = tot; 151 L[R[pos] = tot] = pos; 152 c[tot] = s[i]; 153 pos = tot++; 154 left++; 155 length++; 156 }else if(mode == OVERWITE_MODE && 1 + left <= m) { 157 if(R[pos] != NULL) { 158 L[R[pos] = R[R[pos]]] = pos; 159 length--; 160 } 161 L[R[tot] = R[pos]] = tot; 162 L[R[pos] = tot] = pos; 163 c[tot] = s[i]; 164 pos = tot++; 165 left++; 166 length++; 167 } 168 } 169 if(s[i] != ‘L‘ && s[i] != ‘R‘ && s[i] != ‘C‘) start = -1; 170 } 171 if(R[S] == NULL) { 172 puts("NOTHING"); 173 }else{ 174 for(int i = S; R[i] != NULL; i = R[i]) { 175 putchar(c[R[i]]); 176 } 177 putchar(‘\n‘); 178 } 179 } 180 return 0; 181 }
D. The Celebration of Rabbits
题意:有2*n+1个人,第一遍给每个人先发 [0,m]中任意值的钱,第二遍再给每个人发x元,x的范围为[L,R]。问有多少种第一遍发钱的方法使得:存在x使得这些人的钱的异或值在第二遍发完之后为0。解法:首先暴力打表发现如果某一方案可行,则第二遍发钱的值x一定唯一。(并不会证,但发现2*n+1为奇数时有该性质)
然后就可以转化为:先枚举x,然后求给每个人发[x, x+m]元钱,使得他们异或值为0的方案数。
这个问题可以参照TC SRM 518, 1000的题解。链接
(或参照CF 259 div1 D)
直接枚举x之后使用快速沃尔什变换fwt即可。
注意模数是1e9+7,而fwt逆变换之后需要除掉2n,所以模数要改为(1e9+7) * 2n,导致乘法可能会超long long。如果用快速乘太慢,可以参照下面的写法。
复杂度O((R-L)*M*(logM+logN))
比赛的时候写了一下发现本地大数据跑的有点慢,但是交上去就直接过了。
(听说本题有一个O(N*M)复杂度的算法,并不能想到,orz)
1 #include <bits/stdc++.h> 2 #include <ext/hash_map> 3 #include <ext/hash_set> 4 #include <ext/pb_ds/assoc_container.hpp> 5 #include <ext/pb_ds/tree_policy.hpp> 6 #include <ext/pb_ds/priority_queue.hpp> 7 using namespace std; 8 using namespace __gnu_cxx; 9 using namespace __gnu_pbds; 10 #define XINF INT_MAX 11 #define INF 0x3F3F3F3F 12 #define MP(X,Y) make_pair(X,Y) 13 #define PB(X) push_back(X) 14 #define REP(X,N) for(int X=0;X<N;X++) 15 #define REP2(X,L,R) for(int X=L;X<=R;X++) 16 #define DEP(X,R,L) for(int X=R;X>=L;X--) 17 #define CLR(A,X) memset(A,X,sizeof(A)) 18 #define IT iterator 19 #define RIT reverse_iterator 20 typedef long long ll; 21 typedef unsigned long long ull; 22 typedef pair<int,int> PII; 23 typedef vector<PII> VII; 24 typedef vector<int> VI; 25 typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree_set; 26 typedef tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree; 27 #define PQ std::priority_queue 28 #define HEAP __gnu_pbds::priority_queue 29 #define X first 30 #define Y second 31 #define lson(X) ((X)<<1) 32 #define rson(X) ((X)<<1|1) 33 34 ll mo; 35 36 ll mul(ll a,ll b){ 37 return ((a*b-ll(((long double)a)/mo*b+1e-3)*mo)%mo+mo)%mo; 38 } 39 ll pow_mod(ll a, ll n) { 40 a%=mo; 41 ll r = 1; 42 while(n) { 43 if(n&1) r = mul(r,a); 44 a=mul(a,a); 45 n>>=1; 46 } 47 return r; 48 } 49 void fwt(ll *a,int l,int r){ 50 if(l!=r){ 51 int mid=l+r>>1,len=mid-l+1; 52 fwt(a,l,mid); 53 fwt(a,mid+1,r); 54 for (int i=l;i<=mid;i++){ 55 ll u=a[i],v=a[i+len]; 56 a[i]=(u+v)%mo; 57 a[i+len]=(u-v)%mo; 58 } 59 } 60 } 61 void ifwt(ll *a,int l,int r){ 62 if(l!=r){ 63 int mid=l+r>>1,len=mid-l+1; 64 for (int i=l;i<=mid;i++){ 65 ll u=a[i],v=a[i+len]; 66 a[i]=(u+v)%mo; 67 a[i+len]=(u-v)%mo; 68 } 69 ifwt(a,l,mid); 70 ifwt(a,mid+1,r); 71 } 72 } 73 74 const int M = 1000000007; 75 int N = 2048; 76 77 ll a[2048]; 78 79 ll gao(int L, int R, int n) { 80 N = 1; 81 while(N<=R) N*=2; 82 mo = 1LL * M * N; 83 ll res = 0; 84 REP(i,N) a[i] = i<=R&&i>=L?1:0; 85 fwt(a,0,N-1); 86 REP(i,N) a[i] = pow_mod(a[i], n); 87 ifwt(a,0,N-1); 88 REP(i,N) a[i] = (a[i]+mo)%mo/N; 89 return (a[0]+M)%M; 90 } 91 92 int main() 93 { 94 #ifdef LOCAL 95 freopen("in.txt","r",stdin); 96 #endif // LOCAL 97 int n,m,L,R; 98 while(~scanf("%d%d%d%d",&n,&m,&L,&R)) { 99 n = n*2+1; 100 ll ans = 0; 101 REP2(i,L,R) { 102 ans+=gao(i,m+i,n); 103 ans%=M; 104 } 105 printf("%d\n", (int)ans); 106 } 107 return 0; 108 }
F. Couple Trees
题意:给出两颗大小为N的树A和B,节点标号分别为1..n,且标号小的元素比标号大的元素深度浅。
之后给出Q次查询x, y,要求查询标号最大的点k,使得在A树中k是x的祖先,B树中k是y的祖先。(题目强制在线)
N,M <=100000
解法:先考虑离线解法,则我们对A树dfs,每遍历到一个点就在B树中将这个点标记,回溯的时候将这个标记删除,然后我们在遍历到A树中的x点时,就可以通过维护B树的根到y点的链上所有标记过的点的最大值来计算题意中的查询了。这个方法可以通过树链剖分等完成。
发现这个方法难以拓展为在线算法,于是我们将标记点的做法转化为对这个点的子树中所有点更新一个最大值,然后就可以单点查询B树中点y上方的最大标记点。于是我们发现通过维护B树的dfs序列即可把转化为区间更新最大值,单点查询的简单问题。
之后用主席树(可持久化线段树)就可以轻易转化为在线算法。
复杂度O((N+M)*log(N))
表示本人智商比较捉急,比赛时只能想到树分块的复杂度非常高的算法,没有敢写。
赛后写了一下发现还是比想象中的简单。
1 #include <bits/stdc++.h> 2 #include <ext/hash_map> 3 #include <ext/hash_set> 4 #include <ext/pb_ds/assoc_container.hpp> 5 #include <ext/pb_ds/tree_policy.hpp> 6 #include <ext/pb_ds/priority_queue.hpp> 7 using namespace std; 8 using namespace __gnu_cxx; 9 using namespace __gnu_pbds; 10 #define XINF INT_MAX 11 #define INF 0x3F3F3F3F 12 #define MP(X,Y) make_pair(X,Y) 13 #define PB(X) push_back(X) 14 #define REP(X,N) for(int X=0;X<N;X++) 15 #define REP2(X,L,R) for(int X=L;X<=R;X++) 16 #define DEP(X,R,L) for(int X=R;X>=L;X--) 17 #define CLR(A,X) memset(A,X,sizeof(A)) 18 #define IT iterator 19 #define RIT reverse_iterator 20 typedef long long ll; 21 typedef unsigned long long ull; 22 typedef pair<int,int> PII; 23 typedef vector<PII> VII; 24 typedef vector<int> VI; 25 typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree_set; 26 typedef tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree; 27 #define PQ std::priority_queue 28 #define HEAP __gnu_pbds::priority_queue 29 #define X first 30 #define Y second 31 #define lson(X) ((X)<<1) 32 #define rson(X) ((X)<<1|1) 33 34 #define MAXN 100010 35 #define MAXT (MAXN*80) 36 int n; 37 38 VI Map1[MAXN]; 39 VI Map2[MAXN]; 40 int dep1[MAXN]; 41 int dep2[MAXN]; 42 43 int T[MAXN]; 44 int ls[MAXT]; 45 int rs[MAXT]; 46 int mx[MAXT]; 47 int tot = 0; 48 49 int ins(int i, int L, int R, int l, int r, int x) { 50 l = max(l,L); 51 r = min(r,R); 52 if(l>r) return i; 53 ls[tot] = ls[i]; 54 rs[tot] = rs[i]; 55 mx[tot] = mx[i]; 56 i = tot++; 57 int M = (L+R)>>1; 58 59 if(L == l && R == r) { 60 mx[i] = max(mx[i], x); 61 return i; 62 } 63 ls[i] = ins(ls[i], L, M, l, r, x); 64 rs[i] = ins(rs[i], M+1, R, l, r, x); 65 return i; 66 } 67 68 int query(int i, int L, int R, int x) { 69 int M = (L+R)>>1; 70 int r = mx[i]; 71 if(L == R) return r; 72 if(x<=M) 73 r = max(mx[i], query(ls[i], L, M, x)); 74 else 75 r = max(mx[i], query(rs[i], M+1, R, x)); 76 return r; 77 } 78 79 int s[MAXN]; 80 int e[MAXN]; 81 int dfn = 0; 82 83 void dfs2(int u, int fa = -1) { 84 s[u] = dfn++; 85 REP(i,Map2[u].size()) { 86 int v = Map2[u][i]; 87 if(v == fa) continue; 88 dep2[v] = dep2[u] + 1; 89 dfs2(v, u); 90 } 91 e[u] = dfn-1; 92 } 93 94 void dfs(int u, int fa = -1) { 95 REP(i,Map1[u].size()) { 96 int v = Map1[u][i]; 97 if(v == fa) continue; 98 T[v] = ins(T[u], 0, n-1, s[v], e[v], v); 99 dep1[v] = dep1[u] + 1; 100 dfs(v, u); 101 } 102 } 103 104 int main() 105 { 106 #ifdef LOCAL 107 freopen("in.txt","r",stdin); 108 #endif // LOCAL 109 int m; 110 while(~scanf("%d%d",&n,&m)) { 111 REP(i,n) Map1[i].clear(); 112 REP(i,n) Map2[i].clear(); 113 REP(i,n-1) { 114 int f; 115 scanf("%d",&f); 116 f--; 117 Map1[f].PB(i+1); 118 Map1[i+1].PB(f); 119 } 120 REP(i,n-1) { 121 int f; 122 scanf("%d",&f); 123 f--; 124 Map2[f].PB(i+1); 125 Map2[i+1].PB(f); 126 } 127 dfn = 0; 128 dfs2(0); 129 130 T[0] = 0; 131 ls[0] = rs[0] = 0; 132 mx[0] = 0; 133 tot = 1; 134 135 dfs(0); 136 int last = 0; 137 REP(i,m) { 138 int x,y; 139 scanf("%d%d",&x,&y); 140 x=(x+last)%n; 141 y=(y+last)%n; 142 last = query(T[x],0,n-1,s[y]); 143 printf("%d %d %d\n", last+1, dep1[x]-dep1[last]+1, dep2[y]-dep2[last]+1); 144 last++; 145 } 146 } 147 return 0; 148 }
G. Boxes
题意:给出n个盒子放成一排,每个盒子大小不同,小的盒子只能叠在大的盒子上面,即按照汉诺塔的规则移动这些盒子,使得最后将这些盒子排成从小到大的一排。求最少步数。
N<=7
解法:暴力bfs搜索打表,状态只有NN种,即每个盒子有N个位置可以放,转移的时候大胆地写O(N*N)的也是没有问题的。本地跑出来1秒多,交上去还是200ms+就过了。
这次在hihocoder上,本地跑得慢直接交表的方法也是可以的。这题N=7的时候也只需要一个5040大小的表。
比赛的时候队友状态表示比较复杂,用long long表示状态,答案存在hashmap里,神奇地卡过了。
orz..Java三倍时限跑了2s多
1 #include <bits/stdc++.h> 2 #include <ext/hash_map> 3 #include <ext/hash_set> 4 #include <ext/pb_ds/assoc_container.hpp> 5 #include <ext/pb_ds/tree_policy.hpp> 6 #include <ext/pb_ds/priority_queue.hpp> 7 using namespace std; 8 using namespace __gnu_cxx; 9 using namespace __gnu_pbds; 10 #define XINF INT_MAX 11 #define INF 0x3F3F3F3F 12 #define MP(X,Y) make_pair(X,Y) 13 #define PB(X) push_back(X) 14 #define REP(X,N) for(int X=0;X<N;X++) 15 #define REP2(X,L,R) for(int X=L;X<=R;X++) 16 #define DEP(X,R,L) for(int X=R;X>=L;X--) 17 #define CLR(A,X) memset(A,X,sizeof(A)) 18 #define IT iterator 19 #define RIT reverse_iterator 20 typedef long long ll; 21 typedef unsigned long long ull; 22 typedef pair<int,int> PII; 23 typedef vector<PII> VII; 24 typedef vector<int> VI; 25 typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree_set; 26 typedef tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree; 27 #define PQ std::priority_queue 28 #define HEAP __gnu_pbds::priority_queue 29 #define X first 30 #define Y second 31 #define lson(X) ((X)<<1) 32 #define rson(X) ((X)<<1|1) 33 34 int a[10]; PII b[10]; 35 36 vector<int> mp[10]; 37 38 #define lowbit(x) ((x)&-(x)) 39 40 void gao(int n) { 41 int sz = 1; 42 REP(i,n) sz*=n; 43 mp[n].resize(sz); 44 REP(i,sz) mp[n][i] = -1; 45 46 int s = 0; 47 REP(i,n) 48 s = s * n + i; 49 mp[n][s] = 0; 50 int p[10]; 51 int mn[10]; 52 53 queue<int> q; 54 q.push(s); 55 int cnt = 0; 56 while(!q.empty()) { 57 int x = q.front(); 58 int stp = mp[n][x]; 59 q.pop(); 60 int tx = x; 61 REP(i,n) { 62 p[i] = tx%n; 63 tx /= n; 64 } 65 reverse(p, p+n); 66 REP(i,n) mn[i] = 10; 67 REP(i,n) { 68 mn[p[i]] = min(mn[p[i]], i); 69 } 70 REP(i,n) if(mn[p[i]] == i) { 71 if(p[i] > 0 && mn[p[i]-1] > i) { 72 int xx = 0; 73 REP(j,n) { 74 xx = xx * n + (i==j?p[j]-1:p[j]); 75 } 76 if(mp[n][xx] == -1) { 77 mp[n][xx] = stp+1; 78 q.push(xx); 79 cnt++; 80 } 81 } 82 if(p[i] < n-1 && mn[p[i]+1] > i) { 83 int xx = 0; 84 REP(j,n) { 85 xx = xx * n + (i==j?p[j]+1:p[j]); 86 } 87 if(mp[n][xx] == -1) { 88 mp[n][xx] = stp+1; 89 q.push(xx); 90 cnt++; 91 } 92 } 93 } 94 } 95 } 96 97 int main() 98 { 99 #ifdef LOCAL 100 freopen("in.txt","r",stdin); 101 #endif // LOCAL 102 REP2(i,1,7) gao(i); 103 int t; 104 scanf("%d",&t); 105 while(t--) { 106 int n; 107 scanf("%d", &n); 108 REP(i,n) scanf("%d",&a[i]); 109 REP(i,n) b[i] = MP(a[i],i); 110 sort(b, b+n); 111 int r = 0; 112 REP(i,n) { 113 r = r * n + (b[i].Y); 114 } 115 printf("%d\n", mp[n][r]); 116 } 117 return 0; 118 }
H. Fractal
题意:看图,就是一个1*1的正方形,里面每个线段中点连线再连成一个正方形,不断进行这个操作,然后要求查询与这个正方形的一边平行且距离该边距离为x时,和这个图形有多少个交点。无穷个则输出-1。
0<=x<0.5, x精确到1e-8
解法:直接暴力算个50次就差不多精度小于1e-8了。然后对于每次查询暴力即可。
注意精度问题,比赛时用1e-9的精度没有过。。另外还傻呵呵地去特判了x=0.5的情况。
1 #include <bits/stdc++.h> 2 #include <ext/hash_map> 3 #include <ext/hash_set> 4 #include <ext/pb_ds/assoc_container.hpp> 5 #include <ext/pb_ds/tree_policy.hpp> 6 #include <ext/pb_ds/priority_queue.hpp> 7 using namespace std; 8 using namespace __gnu_cxx; 9 using namespace __gnu_pbds; 10 #define XINF INT_MAX 11 #define INF 0x3F3F3F3F 12 #define MP(X,Y) make_pair(X,Y) 13 #define PB(X) push_back(X) 14 #define REP(X,N) for(int X=0;X<N;X++) 15 #define REP2(X,L,R) for(int X=L;X<=R;X++) 16 #define DEP(X,R,L) for(int X=R;X>=L;X--) 17 #define CLR(A,X) memset(A,X,sizeof(A)) 18 #define IT iterator 19 #define RIT reverse_iterator 20 typedef long long ll; 21 typedef unsigned long long ull; 22 typedef pair<int,int> PII; 23 typedef vector<PII> VII; 24 typedef vector<int> VI; 25 typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree_set; 26 typedef tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update > rb_tree; 27 #define PQ std::priority_queue 28 #define HEAP __gnu_pbds::priority_queue 29 #define X first 30 #define Y second 31 #define lson(X) ((X)<<1) 32 #define rson(X) ((X)<<1|1) 33 34 double x[100]; 35 double y[100]; 36 37 double dist(int i, int j) { 38 return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); 39 } 40 41 vector<pair<double, double> > v; 42 43 int main() 44 { 45 #ifdef LOCAL 46 freopen("in.txt","r",stdin); 47 #endif // LOCAL 48 double a = 0.25; 49 double d = 0; 50 REP(i, 50) { 51 v.PB(MP(d,1-d)); 52 d+=a; 53 a/=2; 54 } 55 56 int t; 57 scanf("%d",&t); 58 while(t--) { 59 double x; 60 scanf("%lf", &x); 61 if(fabs(x-0.5)<1e-10) { 62 puts("1002"); 63 continue; 64 } 65 int infinite = 0; 66 int ans = 0; 67 REP(i,v.size()) { 68 auto p = v[i]; 69 if(x>=p.X && x<=p.Y) ans+=4; 70 if(fabs(v[i].X-x)<1e-10 || fabs(v[i].Y-x)<1e-10) { 71 infinite = 1; 72 } 73 } 74 printf("%d\n", infinite?-1:ans); 75 } 76 return 0; 77 }
标签:
原文地址:http://www.cnblogs.com/curs0r/p/4827046.html