全场梦游。。考研狗好久没码题了已经跪了T T
被自己蠢哭
题目1 : Farthest Point
时间限制:5000ms
单点时限:1000ms
内存限制:256MB
描述
Given a circle on a two-dimentional plane.
Output the integral point in or on the boundary of the circle which has the largest distance from the center.
输入
One line with three floats which are all accurate to three decimal places, indicating the coordinates of the center x, y and the radius r.
For 80% of the data: |x|,|y|<=1000, 1<=r<=1000
For 100% of the data: |x|,|y|<=100000, 1<=r<=100000
输出
One line with two integers separated by one space, indicating the answer.
If there are multiple answers, print the one with the largest x-coordinate.
If there are still multiple answers, print the one with the largest y-coordinate.
- 样例输入
-
1.000 1.000 5.000
- 样例输出
-
6 1
求离圆心最远的整点
思路:
暴力流,注意到X范围有限,枚举之。
接下来有两种方法
1.列方程解出Y,枚举附近点。
2.分上下半圆二分Y
注意精度。。。
#include<bits/stdc++.h> using namespace std; const double eps = 1e-9; const int INF = 0x3f3f3f3f; int dcmp(double x){ if(fabs(x) < eps)return 0; return x < 0 ? -1 : 1; } int main(){ double x, y, r; int left, right; double ansdis = -1; int ansx, ansy; cin >> x>> y >>r; left = ceil(x - r); right = floor(x + r); for(int i = right; i >= left; i--){ int nowx = i; int high = floor(y + r),low = ceil(y),mid; int nowy = -INF; while(low <= high){ mid = (low + high) >> 1; if(dcmp((mid-y)*(mid-y)+(i-x)*(i-x) - r* r) <= 0){ nowy = mid; low = mid + 1; }else high = mid - 1; } if(nowy != -INF){ double nowdis = (nowx-x)*(nowx-x)+(nowy-y)*(nowy-y); if(dcmp(nowdis - ansdis) > 0){ ansx = i; ansy = nowy; ansdis = nowdis; } } nowy = -INF; high = floor(y),low = ceil(y-r); while(low <= high){ mid = (low + high) >> 1; if(dcmp((mid-y)*(mid-y)+(i-x)*(i-x) - r* r) <= 0){ nowy = mid; high = mid - 1; }else low = mid + 1; } if(nowy != -INF){ double nowdis = (nowx-x)*(nowx-x)+(nowy-y)*(nowy-y); if(dcmp(nowdis - ansdis) > 0){ ansx = i; ansy = nowy; ansdis = nowdis; } } } cout << ansx << " " << ansy << endl; return 0; }
题目2 : Total Highway Distance
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Little Hi and Little Ho are playing a construction simulation game. They build N cities (numbered from 1 to N) in the game and connect them by N-1 highways. It is guaranteed that each pair of cities are connected by the highways directly or indirectly.
The game has a very important value called Total Highway Distance (THD) which is the total distances of all pairs of cities. Suppose there are 3 cities and 2 highways. The highway between City 1 and City 2 is 200 miles and the highway between City 2 and City 3 is 300 miles. So the THD is 1000(200 + 500 + 300) miles because the distances between City 1 and City 2, City 1 and City 3, City 2 and City 3 are 200 miles, 500 miles and 300 miles respectively.
During the game Little Hi and Little Ho may change the length of some highways. They want to know the latest THD. Can you help them?
输入
Line 1: two integers N and M.
Line 2 .. N: three integers u, v, k indicating there is a highway of k miles between city u and city v.
Line N+1 .. N+M: each line describes an operation, either changing the length of a highway or querying the current THD. It is in one of the following format.
EDIT i j k, indicating change the length of the highway between city i and city j to k miles.
QUERY, for querying the THD.
For 30% of the data: 2<=N<=100, 1<=M<=20
For 60% of the data: 2<=N<=2000, 1<=M<=20
For 100% of the data: 2<=N<=100,000, 1<=M<=50,000, 1 <= u, v <= N, 0 <= k <= 1000.
输出
For each QUERY operation output one line containing the corresponding THD.
- 样例输入
-
3 5 1 2 2 2 3 3 QUERY EDIT 1 2 4 QUERY EDIT 2 3 2 QUERY
- 样例输出
-
10 14 12 全场最水题?给一棵树,以及边权,定义THD为点两两之间的距离和,一边修改一边查询THD的值思路:另其有根,设深度较深的Y,浅的为X, 则X必为Y的父亲。一条边链接X, Y,则其影响Y子树与外部的距离,即cnt[y]*(n-cnt[y])的贡献。处理出子树规模。记录每个点的深度及通向其父亲的边是哪一条。没了
#include <bits/stdc++.h> using namespace std; const int N = 100005; typedef long long ll; struct Edge{ int x, y, w, next; Edge(){}; Edge(int u, int v, int z, int dis){ x = u, y = v, next = z, w = dis; } }edge[N<<1]; int cntson[N], head[N], deep[N]; int faedge[N]; int no; ll ans; void init(){ memset(head, -1, sizeof(head)); memset(cntson, 0, sizeof(cntson)); no = 0; } void add(int x, int y, int z){ edge[no] = Edge(x, y , head[x], z); head[x] = no++; edge[no] = Edge(y, x, head[y], z); head[y] = no++; } void dfs(int x, int fa){ int i, y; cntson[x] = 1; for(i = head[x]; i != -1; i = edge[i].next){ y = edge[i].y; if(y == fa)continue; deep[y] = deep[x] + 1; dfs(y, x); cntson[x] += cntson[y]; faedge[y] = i; } } int main(){ int n, m, x, y, z, i; char op[10]; init(); scanf("%d%d", &n, &m); for(i = 1; i < n; i++){ scanf("%d%d%d", &x, &y, &z); add(x, y, z); } deep[1] = 0; dfs(1, 1); for(i = 0; i < no; i+=2){ x = edge[i].x; y = edge[i].y; if(deep[x] > deep[y]) swap(x, y); ans += (ll)(cntson[y])*(n - cntson[y])*edge[i].w; } while(m--){ scanf("%s", op); if(op[0] == ‘Q‘){ printf("%lld\n", ans); }else{ scanf("%d%d%d",&x, &y, &z); if(deep[x] > deep[y]) swap(x, y); i = faedge[y]; ans += (ll)(z - edge[i].w) * (cntson[y])*(n - cntson[y]); edge[i].w = z; } } return 0; }
题目3 : Fibonacci
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Given a sequence {an}, how many non-empty sub-sequence of it is a prefix of fibonacci sequence.
A sub-sequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
The fibonacci sequence is defined as below:
F1 = 1, F2 = 1
Fn = Fn-1 + Fn-2, n>=3
输入
One line with an integer n.
Second line with n integers, indicating the sequence {an}.
For 30% of the data, n<=10.
For 60% of the data, n<=1000.
For 100% of the data, n<=1000000, 0<=ai<=100000.
输出
One line with an integer, indicating the answer modulo 1,000,000,007.
样例提示
The 7 sub-sequences are:
{a2}
{a3}
{a2, a3}
{a2, a3, a4}
{a2, a3, a5}
{a2, a3, a4, a6}
{a2, a3, a5, a6}
- 样例输入
-
6 2 1 1 2 2 3
- 样例输出
-
7 处理出所有FIB值。对于Ai,若其为FIB,则求出她是第几个(fibj),设cnt[j-1]为所有长度为j-1的符合题意的序列个数。则cnt[j] += cnt[j-1];特别处理一下x=1的时候。。 最后统计所有cnt就行了。注意取模= =
#include<bits/stdc++.h> using namespace std; const int N = 33; const int MOD = 1000000007; typedef long long ll; int fib[N]; ll cnt[N]; bool vis[N]; int main(){ int n, i,j, x; memset(fib, -1, sizeof(fib)); memset(vis, 0, sizeof(vis)); fib[1] = 1; fib[2] = 1; for(i = 3; fib[i-1] <= 100000; i++){ fib[i] = fib[i-1] + fib[i-2]; //printf("%d %d\n", i, fib[i]); } //fib[25] //1!!!!!!!!!!!!!! memset(cnt, 0, sizeof(cnt)); ll ans = 0; scanf("%d", &n); for(i = 1; i <= n; i++){ scanf("%d", &x); if(x == 1){ cnt[2] =(cnt[1] + cnt[2]) % MOD; cnt[1] ++; if(cnt[2] >=1) vis[2] = true; continue; } ll tmp = 1; for( j = 3; j <= 25 && fib[j] <= x; j++){ if(x == fib[j]) break; } if(x == fib[j]) { if(vis[j-1]){ cnt[j] = (cnt[j] + cnt[j-1]) % MOD; vis[j] = true; } } // cout << i << " " << ans<<endl; } for(i = 1; i <= 25; i++){ ans = (ans + cnt[i]) % MOD; } cout << ans<<endl; return 0; }
题目4 : Image Encryption
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
A fancy square image encryption algorithm works as follow:
0. consider the image as an N x N matrix
1. choose an integer k∈ {0, 1, 2, 3}
2. rotate the square image k * 90 degree clockwise
3. if N is odd stop the encryption process
4. if N is even split the image into four equal sub-squares whose length is N / 2 and encrypt them recursively starting from step 0
Apparently different choices of the k serie result in different encrypted images. Given two images A and B, your task is to find out whether it is POSSIBLE that B is encrypted from A. B is possibly encrypted from A if there is a choice of k serie that encrypt A into B.
输入
Input may contains multiple testcases.
The first line of the input contains an integer T(1 <= T <= 10) which is the number of testcases.
The first line of each testcase is an integer N, the length of the side of the images A and B.
The following N lines each contain N integers, indicating the image A.
The next following N lines each contain N integers, indicating the image B.
For 20% of the data, 1 <= n <= 15
For 100% of the data, 1 <= n <= 100, 0 <= Aij, Bij <= 100000000
输出
For each testcase output Yes or No according to whether it is possible that B is encrypted from A.
- 样例输入
-
3 2 1 2 3 4 3 1 4 2 2 1 2 4 3 3 1 4 2 4 4 1 2 3 1 2 3 4 2 3 4 1 3 4 1 2 3 4 4 1 2 3 1 2 1 4 4 3 2 1 3 2
- 样例输出
-
Yes No Yes 赛时犯了一个极其脑残的错误导致样例都过不去。。。泪目。。。。。。。。。。T T暴力了一个写法,不知道能不能过。等题库放题了再改吧
#include <bits/stdc++.h> using namespace std; const int N = 104; int gox[4] = {1, 1, -1, -1}; int goy[4] = {1, -1, -1, 1}; struct Matrix{ int f[N][N]; int mat_size; void scan(){ int i, j; for(i = 1; i <= mat_size; i++) for(j = 1; j <= mat_size; j++) scanf("%d", &f[i][j]); } void print(){ int i, j; for(i = 1; i <= mat_size; i++){ for(j = 1; j <= mat_size; j++) printf("%d ", &f[i][j]); printf("\n"); } } void modify(int t, int ra, int rb, int ca, int cb){ Matrix C; int i, j, ii, jj; if(t & 1){ if(t == 1){ for(j = cb, ii = ra; j >= ca; j--, ii++) for(i = ra, jj = ca; i <= rb; i++, jj++) C.f[ii][jj] = f[i][j]; }else{ for(j = ca, ii = ra; j <= cb; j++, ii++) for(i = rb, jj = ca; i >= ra; i--, jj++) C.f[ii][jj] = f[i][j]; } } else{ for(i = rb, ii = ra; i >= ra; i--, ii++) for(j = cb, jj = ca; j >= ca; j--, jj++) C.f[ii][jj] = f[i][j]; } for(i = ra; i <= rb; i++) for(j = ca; j <= cb; j++) f[i][j] = C.f[i][j]; } bool check(const Matrix &I, int ra, int rb, int ca, int cb)const{ int i, j; for(i = ra; i <= rb; i++) for(j = ca; j <= cb; j++) if(f[i][j] != I.f[i][j]) return false; return true; } }A, B; bool judge(Matrix &A, Matrix &B, int ra, int rb, int ca, int cb){ int i; Matrix Tmp = A; for(i = 0; i < 4; i++){ if(i != 0) A.modify(i, ra, rb, ca, cb); if(((ra - rb + 1) &1 )){ if(B.check(A, ra, rb, ca, cb)) return true; }else{ if(B.check(A, ra, rb, ca, cb))return true; if(judge(A, B, ra, (ra + rb) / 2, ca, (ca+cb)/2) && judge(A, B, (ra + rb) / 2+1, rb, (ca + cb) / 2 + 1, cb)&& judge(A, B, ra, (ra + rb) / 2, (ca + cb) / 2 + 1, cb) && judge(A, B, (ra + rb) / 2+1, rb, ca, (ca+cb)/2)) return true; } A = Tmp; } return false; } int main(){ int n, TC, i, j; scanf("%d", &TC); while(TC--){ scanf("%d", &n); A.mat_size = B.mat_size = n; A.scan(); B.scan(); printf("%s\n", judge(A, B, 1, n, 1, n)?"Yes" : "No"); // A = A.modify(1, 1,n, 1, n); // A.print(); } return 0; }