723A The New Year: Meeting Friends
题目大意:数轴上有3个点
x1<x2<x3
,求一个点
x0
,最小化
|x1−x0|+|x2−x0|+|x3−x0|
。
题解:显然,
x0
肯定在
[x1,x3]
,否则不是最优解,故化简得:
x3−x1+|x2−x0|
,故
x0=x2
时解最小为
x3−x1
#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
int x[3];
scanf("%d%d%d", x, x + 1, x + 2);
sort(x, x + 3);
printf("%d", x[2] - x[0]);
return 0;
}
723B Text Document Analysis
题目大意:找出在括号外的最长单词的长度和括号内的单词个数
题解:简单模拟一下就好了
#include <stdio.h>
int max(int a, int b) { return a > b ? a : b; }
char s[256];
int main() {
int n, i, l = 0, p = 0, cnt = 0, ans1 = 0, ans2 = 0;
scanf("%d%s", &n, s);
s[n] = 0;
for (i = 0; i <= n; ++i) {
if ('A' <= s[i] && s[i] <= 'Z' ||
'a' <= s[i] && s[i] <= 'z')
++l;
else {
if (l) {
if (p) ++ans2;
else ans1 = max(ans1, l);
l = 0;
}
if (s[i] == '(') ++p;
else if (s[i] == ')') --p;
}
}
printf("%d %d", ans1, ans2);
return 0;
}