CCPC广州热身赛
A
预选赛原题
暴力分类讨论即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 65;
int sgn[N], a[N], b[N], c[N], add, sub;
int n;
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i++) scanf("%d", &sgn[i]);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
for(int i = 0; i < n; i++) scanf("%d", &b[i]);
for(int i = 0; i < n; i++)
{
if(add && sub) {add = 0; sub = 0;}
if(sgn[i] == 1)
{
if(!add && !sub)
{
if(a[i] == 0 && b[i] == 0) c[i] = 0;
else if(a[i] == 1 && b[i] == 0) c[i] = 1;
else if(a[i] == 0 && b[i] == 1) c[i] = 1;
else if(a[i] == 1 && b[i] == 1) {c[i] = 0; add = 1;}
}
else if(add && !sub)
{
if(a[i] == 0 && b[i] == 0) {c[i] = 1; add = 0;}
else if(a[i] == 1 && b[i] == 0) {c[i] = 0;}
else if(a[i] == 0 && b[i] == 1) c[i] = 0;
else c[i] = 1;
}
else if(!add && sub)
{
if(a[i] == 0 && b[i] == 0) {c[i] = 1;}
else if(a[i] == 1 && b[i] == 0) {sub = 0; c[i] = 0;}
else if(a[i] == 0 && b[i] == 1) {sub = 0; c[i] = 0;}
else {c[i] = 1; sub = 0;}
}
}
else if(sgn[i] == -1)
{
if(!add && !sub)
{
if(a[i] == 0 && b[i] == 0) c[i] = 0;
else if(a[i] == 1 && b[i] == 0) c[i] = 1;
else if(a[i] == 0 && b[i] == 1) c[i] = 1;
else if(a[i] == 1 && b[i] == 1) {c[i] = 0; sub = 1;}
}
else if(add && !sub)
{
if(a[i] == 0 && b[i] == 0) {c[i] = 1;}
else if(a[i] == 1 && b[i] == 0) {c[i] = 0; add = 0;}
else if(a[i] == 0 && b[i] == 1) {c[i] = 0; add = 0;}
else {c[i] = 1; add = 0;}
}
else if(!add && sub)
{
if(a[i] == 0 && b[i] == 0) {c[i] = 1; sub = 0;}
else if(a[i] == 1 && b[i] == 0) {sub = 1; c[i] = 0;}
else if(a[i] == 0 && b[i] == 1) {sub = 1; c[i] = 0;}
else {c[i] = 1; sub = 1;}
}
}
}
for(int i = 0; i < n - 1; i++) printf("%d ", c[i]);
printf("%d", c[n - 1]);
return 0;
}
B
模拟,不断旋转。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 35;
int n;
int a1[N][N];
int a2[N][N];
int b1[N][N], b2[N][N], b3[N][N];
int main()
{
while(scanf("%d", &n)) {
if(!n) break;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
scanf("%d", &a1[i][j]);
}
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
scanf("%d", &a2[i][j]);
}
}
int ans = 0;
int tmp = 0;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
if(a1[i][j] == a2[i][j]) {
tmp ++;
}
}
}
ans = max(ans, tmp);
tmp = 0;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
b1[i][j] = a1[n - j + 1][i];
}
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
if(b1[i][j] == a2[i][j]) {
tmp ++;
}
}
}
ans = max(ans, tmp);
tmp = 0;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
b2[i][j] = b1[n - j + 1][i];
}
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
if(b2[i][j] == a2[i][j]) {
tmp ++;
}
}
}
ans = max(ans, tmp);
tmp = 0;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
b3[i][j] = b2[n - j + 1][i];
}
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
if(b3[i][j] == a2[i][j]) {
tmp ++;
}
}
}
ans = max(ans, tmp);
printf("%d\n", ans);
}
return 0;
}
C
计算几何
求三位平面两条直线的公垂线及长度即交点坐标
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int T;
double xa1, ya1, za1, xa2, ya2, za2;
double xb1, yb1, zb1, xb2, yb2, zb2;
inline double Sqr(double a)
{
return a * a;
}
inline double Sqrt(double a)
{
return a <= 0 ? 0 : sqrt(a);
}
struct Point_3 {
double x, y, z;
Point_3() {}
Point_3(double x, double y, double z) : x(x), y(y), z(z) {}
Point_3 operator - (const Point_3 &b)
{
return Point_3(x - b.x, y - b.y, z - b.z);
}
Point_3 operator * (const double &k)
{
return Point_3(k * x, k * y, k * z);
}
Point_3 operator + (const Point_3 &b)
{
return Point_3(x + b.x, y + b.y, z + b.z);
}
};
double dis(const Point_3 &a, const Point_3 &b)
{
return Sqrt(Sqr(a.x - b.x) + Sqr(a.y - b.y) + Sqr(a.z - b.z));
}
struct Line_3 {
Point_3 a, b;
Line_3() {}
Line_3(Point_3 a, Point_3 b) : a(a), b(b) {}
};
struct Plane_3{
Point_3 a, b, c;
Plane_3() {}
Plane_3(Point_3 a, Point_3 b, Point_3 c) : a(a), b(b), c(c) {}
};
Point_3 det(const Point_3 &a, const Point_3 &b)
{
return Point_3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
Point_3 pvec(Point_3 s1, Point_3 s2, Point_3 s3)
{
return det((s1 - s2), (s2 - s3));
}
Point_3 intersection(Line_3 l, Plane_3 s)
{
Point_3 ret = pvec(s.a, s.b, s.c);
double t = (ret.x * (s.a.x - l.a.x) + ret.y * (s.a.y - l.a.y) + ret.z * (s.a.z - l.a.z)) /
(ret.x * (l.b.x - l.a.x) + ret.y * (l.b.y - l.a.y) + ret.z * (l.b.z - l.a.z));
ret = l.a + (l.b - l.a) * t;
return ret;
}
int main()
{
scanf("%d", &T);
while(T --)
{
cin >> xa1 >> ya1 >> za1 >> xa2 >> ya2 >> za2;
cin >> xb1 >> yb1 >> zb1 >> xb2 >> yb2 >> zb2;
Point_3 p1 = Point_3(xa1, ya1, za1), p2 = Point_3(xa2, ya2, za2);
Point_3 p3 = Point_3(xb1, yb1, zb1), p4 = Point_3(xb2, yb2, zb2);
Point_3 p5 = p1 - p2, p6 = p3 - p4;
Point_3 n = det(p5, p6);
//printf("n : %.6f %.6f %.6f\n", n.x, n.y, n.z);
Plane_3 P1 = Plane_3(p1, p2, p1 + n), P2 = Plane_3(p3, p4, p3 + n);
Line_3 l1 = Line_3(p1, p2), l2 = Line_3(p3, p4);
Point_3 ans1 = intersection(l1, P2), ans2 = intersection(l2, P1);
double res = dis(ans1, ans2);
printf("%.6f\n", res);
printf("%.6f %.6f %.6f\n", ans1.x, ans1.y, ans1.z);
printf("%.6f %.6f %.6f\n", ans2.x, ans2.y, ans2.z);
}
return 0;
}