1、CodeForces 337A Puzzles
题意:
①在有m个整数的集合中
②找到有n个整数的子集
③再比较这些子集中元素的最大值与最小值的差值
④输出最小的差值
解题思路:
①将集合中的元素从大到小排序,就可以把集合看成一个有序的序列
②比较长度为n的子序列的 “头” 和 “尾” 的差
我:
训练开始不久很多人都AC了
面对这种题我的思绪已经混乱了…
看了别人代码才懂的
用sort做的时候写比较函数写错了,写成下面这样
int cmp_num(int x, int y)
{
return (x - y);
}
WA一次
练得太少,尤其是思维锻炼
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 50 + 5;
int f[maxn];
int cmp_num(int x, int y)
{
return (x > y);
}
int main()
{
// freopen("in.txt", "r", stdin);
int n, m;
while (cin>>n>>m) {
for (int i=0; i<m; ++i) {
cin>>f[i];
}
sort(f, f+m, cmp_num);
int Min = INT_MAX;
for (int i=0, j=n-1; j<m; ++i,++j) {
int t = f[i] - f[j];
if (t < Min) {
Min = t;
}
}
cout<<Min<<endl;
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int maxn = 50 + 5;
int f[maxn];
int cmp_num(const void *a, const void *b)
{
return *(int *)b - *(int *)a;
}
int main()
{
// freopen("in.txt", "r", stdin);
int n, m;
while (cin>>n>>m) {
for (int i=0; i<m; ++i) {
cin>>f[i];
}
qsort(f, m, sizeof(f[0]), cmp_num);
int Min = INT_MAX;
for (int i=0, j=n-1; j<m; ++i,++j) {
int t = f[i] - f[j];
if (t < Min) {
Min = t;
}
}
cout<<Min<<endl;
}
return 0;
}
2、CodeForces 520A Pangram
题意:
给一个长度为n的字符串,判断26个英文字母(不论大小写)是否都出现过
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
char s[150];
int vis[30];
int main()
{
// freopen("in.txt", "r", stdin);
int n;
while (cin>>n) {
memset(vis, 0, sizeof(vis));
int Count = 0;
for (int i=0; i<n; ++i) {
cin>>s[i];
if (s[i]>='A' && s[i]<='Z') {
s[i] += ('a' - 'A');
}
if (!vis[s[i]-'a']) {
vis[s[i]-'a'] = 1;
++Count;
}
}
if (Count == 26) {
cout<<"YES"<<endl;
} else {
cout<<"NO"<<endl;
}
}
return 0;
}
3、CodeForces 635A Orchestra
题意:
①给一个r行c列的图
②对图中的n个点进行标记
③找出至少包含k个被标记点的子矩形
解题思路:暴力
①遍历图中所有的点
②找到以该点为左上角的所有矩形
③再遍历这些矩形中的所有点进行判断
我:
CF的A题,搞了几个小时…..
注意有可能输入的k可能和下面定义的k混淆
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 10 + 5;
int Graph[maxn][maxn];
int main()
{
// freopen("in.txt", "r", stdin);
int r, c, n, K;
while (cin>>r>>c>>n>>K) {
memset(Graph, 0, sizeof(Graph));
int x, y;
for (int i=0; i<n; ++i) {
cin>>x>>y;
Graph[x][y] = 1;
}
int num = 0;
int i, j, k, l, a, b;
for (i=1; i<=r; ++i) {
for (j=1; j<=c; ++j) {
for (k=i; k<=r; ++k) {
for (l=j; l<=c; ++l) {
int Count = 0;
for (a=i; a<=k; ++a) {
for (b=j; b<=l; ++b) {
if (Graph[a][b] == 1) {
++Count;
}
}
}
if (Count >= K) {
// cout<<i<<" "<<j<<" - "<<k<<" "<<l<<endl;
++num;
}
}
}
}
}
cout<<num<<endl;
}
return 0;
}
4、HDU 1597 find the nth digit
感觉挂这道题是用来练习二分的,但是我们大多人都是用了比较简单的数学方法
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
// freopen("in.txt", "r", stdin);
int K;
while (cin>>K) {
while (K--) {
int N;
cin>>N;
int x = 1;
while (N > x) {
N -= x;
++x;
}
if (N % 9 == 0) {
cout<<9<<endl;
} else {
cout<<N%9<<endl;
}
}
}
return 0;
}
5、hihoCoder 1245 王胖浩与三角形
题意:
①有一个三角形,三边长为a,b,c
②可以增加三条边的边长,增加的总长度不能超过l
③求三角形最大的面积
解题思路:
让三个边的差值尽可能的小
我:
第一次AC:
提交时间:2015-12-22 17:17:25
第二次AC:
提交时间:2016-03-29 15:27:38
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
double S(double a,double b,double c)
{
double p;
double s;
p = (a + b + c) / 2.0;
s = sqrt(p * (p-a) * (p-b) * (p-c));
return s;
}
int main()
{
int T;
double a,b,c,l,s,M,L;
double area;
int i;
cout.setf(ios::fixed);
while (cin>>T) {
while (T--) {
cin>>a>>b>>c>>l;
s = L = a;
if (b > L) {
L = b;
}
if (b < s) {
s = b;
}
if (c > L) {
L = c;
}
if (c < s) {
s = c;
}
M = a + b + c - s - L;
if (l-(M-s) > 0) {
l -= (M-s);
s = M;
} else {
s += l;
l = 0;
}
if (l>0.0 && l>(2.0*(L-M))) {
l -= (2.0*(L-M));
s = M = L;
} else {
s += (l/2.0);
M += (l/2.0);
l = 0;
}
if (l>0.0) {
s += (l/3.0);
M += (l/3.0);
L += (l/3.0);
}
// cout<<s<<" "<<M<<" "<<L<<endl;
area = S(s,M,L);
cout<<setprecision(10)<<area<<endl;
}
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
#define Max(a, b) ((a > b) ? a : b)
#define Min(a, b) ((a < b) ? a : b)
double S(double a, double b ,double c);
//double area(double x1, double y1, double x2, double y2, double x3, double y3);
int main()
{
// freopen("in.txt", "r", stdin);
int T;
cout.setf(ios::fixed);
while (cin>>T) {
while (T--) {
double a, b, c, l;
cin>>a>>b>>c>>l;
double high, mid, low;
high = Max(Max(a, b), Max(b, c));
low = Min(Min(a, b), Min(b, c));
mid = (a+b+c) - (high+low);
double t = mid - low;
if (l > t) {
l -= t;
low = mid;
} else {
low += l;
l = 0;
}
t = 2.0 * (high - mid);
if (l > t) {
l -= t;
low = mid = high;
} else {
low += l/2.0;
mid += l/2.0;
l = 0;
}
if (l > 0) {
low += (l/3.0);
mid += (l/3.0);
high += (l/3.0);
}
cout<<setprecision(10)<<S(low, mid, high)<<endl;
}
}
return 0;
}
double S(double a, double b, double c)
{
double t;
double s;
t = (a + b + c) / 2.0;
s = sqrt(t * (t-a) * (t-b) * (t-c));
return s;
}
[6、团体程序设计天梯赛-练习集 L2-008 最长对称子串](https://www.patest.cn/contests/gplt/L2-008)
------------------------------------------------------------------------
暴力搜
include
include
include
include
using namespace std;
typedef long long ll;
const int maxn = 1000 + 10;
char s[maxn];
int main()
{
gets(s);
int Max = 1;
int len = strlen(s);
for (int i = 0; i < len; ++i) {
int t;
if (i+1 < len && s[i] == s[i+1]) {
t = 2;
for (int j = i-1, k = i+2; j>=0 && k < len && s[j] == s[k]; –j, ++k) {
t += 2;
}
if (t > Max) {
Max = t;
}
}
if (i+2 < len && s[i] == s[i+2]) {
t = 3;
for (int j = i-1, k = i+3; j>=0 && k < len && s[j] == s[k]; –j, ++k) {
t += 2;
}
if (t > Max) {
Max = t;
}
}
}
printf(“%d\n”, Max);
return 0;
}
[7、团体程序设计天梯赛-练习集 L1-006 连续因子](https://www.patest.cn/contests/gplt/L1-006)
------------------------------------------------------------------------
暴力搜
时间复杂度O(sqrt(n))
include
include
include
include
include
include
include
include
include
include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const double eps = 1e-8;
const int INF = 0x7fffffff;
const int maxn = 1000;
int vis[maxn];
int num[maxn];
int main()
{
int N;
scanf(“%d”, &N);
int t = sqrt(N) + 1;
bool flag = true;
int Max = 0;
int key;
int Max_t;
for (int i = 2; i < t; ++i) {
Max_t = 0;
if (N%i == 0) {
flag = false;
int mul = i;
int add = 1;
while (N%mul == 0) {
++Max_t;
mul *= (i+add);
++add;
}
if (Max_t > Max) {
Max = Max_t;
key = i;
}
}
}
if (flag) {
printf(“1\n”);
printf(“%d\n”, N);
} else {
printf(“%d\n”, Max);
printf(“%d”, key);
for (int i = 0; i < Max - 1; ++i) {
printf(“*%d”, ++key);
}
printf(“\n”);
}
return 0;
}
[8、团体程序设计天梯赛-练习集 L1-009 N个数求和](https://www.patest.cn/contests/gplt/L1-009)
------------------------------------------------------------------------
注意输出细节
include
include
include
include
include
include
include
include
include
include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const int mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 100 + 10;
ll a[maxn], b[maxn];
ll gcd(ll a, ll b);
int main()
{
int N;
scanf(“%d”, &N);
for (int i = 0; i < N; ++i) {
scanf(“%lld/%lld”, &a[i], &b[i]);
}
ll d = abs(gcd(a[0], b[0]));
ll ans_a = a[0] / d;
ll ans_b = b[0] / d;
for (int i = 1; i < N; ++i) {
ll lcm = ans_b / gcd(ans_b, b[i]) * b[i];
ans_a = ans_a * (lcm / ans_b) + a[i] * (lcm / b[i]);
ans_b = lcm;
d = abs(gcd(ans_a, ans_b));
ans_a /= d;
ans_b /= d;
}
ll t1 = ans_a / ans_b;
ll t2 = ans_a % ans_b;
if (t1 != 0 && t2 != 0) {
printf(“%lld %lld/%lld\n”, t1, t2, ans_b);
} else if (t2 != 0) {
printf(“%lld/%lld\n”, t2, ans_b);
} else if (t1 != 0) {
printf(“%lld\n”, t1);
} else {
printf(“0\n”);
}
return 0;
}
ll gcd(ll a, ll b)
{
return (a%b == 0) ? b : gcd(b, a%b);
}
[9、CodeForces_680A Bear and Five Cards](http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=399076)
------------------------------------------------------------------------
题意:
输入 5 个数,求减去 2 个或 3 个相同的数后的最小值
include
include
include
include
include
include
include
include
include
include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const int mod = 1e9 + 7;
const int INF = 0x7fffffff;
int t[10];
int vis[110];
bool vis_t[110];
int main()
{
memset(vis, 0, sizeof(vis));
memset(vis, false, sizeof(vis_t));
int sum = 0;
for (int i = 1; i <= 5; ++i) {
scanf(“%d”, &t[i]);
++vis[t[i]];
sum += t[i];
}
int Min = sum;
for (int i = 1; i < 5; ++i) {
if (vis[t[i]] >= 3 && !vis_t[t[i]]) {
if (Min > sum - t[i] * 3) {
Min = sum - t[i] * 3;
}
vis_t[t[i]] = true;
} else if (vis[t[i]] >= 2 && !vis_t[t[i]]) {
if (Min > sum - t[i] * 2) {
Min = sum - t[i] * 2;
}
vis_t[t[i]] = true;
}
}
printf(“%d\n”, Min);
return 0;
}
[10、CodeForces_680B Bear and Finding Criminals](http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=399032)
------------------------------------------------------------------------
题意:
给出 n 个城市中每个城市罪犯的数量(最多为 1)和警察的位置
若警察知道与他隔某个距离的位置有多少个罪犯(这个位置可能为2)
求他能锁定多少个罪犯
解题思路:
首先他的位置若有罪犯,他就可以锁定一个
然后左右两边距离为 n 的位置都有城市,那么只有两个城市都有罪犯时才能锁定两个罪犯
若只有左边或右边有城市,那么只要这个城市有罪犯就可以锁定
include
include
include
include
include
include
include
include
include
include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const int mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 100 + 10;
int t[maxn];
int main()
{
int n, a;
scanf(“%d%d”, &n, &a);
for (int i = 1; i <= n; ++i) {
scanf(“%d”, &t[i]);
}
int Count = 0;
if (t[a] == 1) {
++Count;
}
int Left = a-1, Right = a+1;
while (Left >= 1 || Right <= n) {
if (Left >= 1 && Right <= n) {
if (t[Left] == 1 && t[Right] == 1) {
Count += 2;
}
–Left;
++Right;
} else if (Left >= 1) {
if (t[Left] == 1) {
++Count;
}
–Left;
} else if (Right <= n) {
if (t[Right] == 1) {
++Count;
}
++Right;
}
}
printf(“%d\n”, Count);
return 0;
}
“`