标签:
给你图中一点求出它周围有几个可达的点;
除边界之外都是8个,边界处理一下即可;
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cctype> #include<queue> #include<stack> #include<vector> #include<algorithm> using namespace std; typedef long long LL; #define N 100100 #define INF 0x3f3f3f3f int main() { char a; int b, ans; scanf("%c%d", &a, &b); if(a == ‘a‘ || a == ‘h‘) { if(b == 8 || b == 1) ans = 3; else ans = 5; } else { if(b == 8 || b == 1) ans = 5; else ans = 8; } printf("%d\n", ans); return 0; }
在坐标抽上给你n个点,选出一个点,使得所有点到这点的距离和最小;
排序输出中间值即可;
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cctype> #include<queue> #include<stack> #include<vector> #include<algorithm> using namespace std; typedef long long LL; #define N 300100 #define INF 0x3f3f3f3f int a[N]; int main() { int n; scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d", &a[i]); sort(a+1, a+n+1); printf("%d\n", a[(n+1)/2]); return 0; } /* 7 1 2 3 5 8 99 100 6 3 2 1 98 99 100 */
给你一个奇数n(n<=49)然后把1---n*n这些数填入到n*n的矩阵中,使得每行每列以及两个主对角线的和都为奇数
我们可以发现规律就是在矩阵的中间那个菱形地方填入奇数就能满足条件;
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cctype> #include<queue> #include<stack> #include<vector> #include<algorithm> using namespace std; typedef long long LL; #define N 300 #define INF 0x3f3f3f3f int main() { int n, x = 1, y = 2,a[N][N]; scanf("%d", &n); int half = (n+1)/2; for(int i=1; i<=half; i++) { for(int j=half-i+1; j<=half+i-1; j++) { a[i][j] = x; x += 2; } } for(int i=n,m=1; i>half; i--,m++) { for(int j=half-m+1; j<=half+m-1; j++) { a[i][j] = x; x += 2; } } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(a[i][j] == 0) { a[i][j] = y; y += 2; } printf("%d%c", a[i][j], j==n?‘\n‘:‘ ‘); } } return 0; }
想输入一个长度为 n 字符串,字符串中的字符都是一样的,添加或者删除一个字符各需要x秒,复制并且粘贴共需要y秒,问最少需要多少秒能把n个字符都打出来;
当n为偶数时,n可以由n/2复制得来,也可以由添加n/2个字符得来,所以取两者中的最小值;
当n为奇数是,n要删除一个,或者添加一个字符,所以我们要看两种哪种带来的时间少,谁少取谁;
用递归一层一层的找下去,用find(n)返回到达n所需的最优解;
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <map> using namespace std; #define N 2010 #define met(a, b) memset(a, b, sizeof(a)) #define INF 0x3f3f3f3f #define LINF 10e16 typedef long long LL; LL x, y; LL Find(LL n) { if(n == 0) return 0; if(n == 1) return x;///必须由添加一个字符得来; if(n%2 == 0) { LL num = Find(n/2); return min(num + x*(n/2), num + y); } else { LL num1 = Find(n-1); LL num2 = Find(n+1); return min(num1, num2) + x; } } int main() { LL n; scanf("%I64d %I64d %I64d", &n, &x, &y); LL ans = Find(n); printf("%I64d\n", ans); return 0; }
Educational Codeforces Round 16---部分题解
标签:
原文地址:http://www.cnblogs.com/zhengguiping--9876/p/5798299.html