这一题就是遍历就行。
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <stdio.h>
#include <numeric>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
typedef unsigned char uchar;
#define G_DEBUG
int dirs[8][2] = { -1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1 };
int main()
{
#ifndef G_DEBUG
int n = 0, k = 0;
scanf("%d %d", &n, &k);
vector<int> nums(n, 0);
vector<int> t(n, 0);
int result = 0;
for (int i = 0; i < n; ++i)
scanf("%d", &nums[i]);
for (int i = 0; i < n; ++i)
{
scanf("%d", &t[i]);
if (t[i] == 1)
result += nums[i];
}
#else
int n = 6, k = 3;
int result = 0;
vector<int> nums = { 1, 3, 5, 2, 5, 4 };
vector<int> t = { 1, 1, 0, 1, 0, 0 };
for (int i = 0; i < n; ++i)
{
if (t[i] == 1)
result += nums[i];
}
#endif
int add_max = 0;
for (int i = 0; i <= n - k; ++i)
{
int temp = 0;
for (int j = i; j < i+k; ++j)
{
if (t[j] == 0)
temp += nums[j];
}
add_max = max( temp, add_max );
}
result += add_max;
printf("%d\n", result);
system("pause");
return 0;
}
这一题是后来结束之后弄出来的,之前的正方形判断写错了23333
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <stdio.h>
#include <numeric>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
typedef unsigned char uchar;
// #define G_DEBUG
struct point
{
double x, y;
} a[4];
bool cmp(point a, point b)
{
if (a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
double TwoPointDiatance(point a, point b)
{
return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2));
}
bool IsRightAngle(point a, point b, point c)
{
double x;
x = (a.x - b.x)* (a.x - c.x) + (a.y - b.y)*(a.y - c.y);
if (x < 0.00001)
return 1;
else
return 0;
}
// 逆时针旋转90度
void trans( vector<int>& num )
{
int xx = num[3] + num[2] - num[1];
int yy = num[0] - num[2] + num[3];
num[0] = xx;
num[1] = yy;;
}
// reference : https://blog.csdn.net/qq_29567701/article/details/79676736
bool isSquare(vector<vector<int>>& nums)
{
double s1, s2, s3, s4;
for (int i = 0; i < 4; ++i)
{
a[i].x = nums[i][0];
a[i].y = nums[i][1];
}
sort( a, a+4, cmp );
s1 = TwoPointDiatance(a[0], a[2]);
s2 = TwoPointDiatance(a[0], a[1]);
s3 = TwoPointDiatance(a[3], a[1]);
s4 = TwoPointDiatance(a[2], a[3]);
bool ret = (s1 == s2&&s3 == s4&&s1 == s2&&s1 != 0 && IsRightAngle(a[0], a[1], a[2]));
return ret;
}
int main()
{
#ifndef G_DEBUG
int n = 0;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
int result = MOD;
vector<vector<int>> nums(4, vector<int>(4, 0));
for (int j = 0; j < 4; j++)
{
scanf("%d %d %d %d", &nums[j][0], &nums[j][1], &nums[j][2], &nums[j][3]);
}
//vector<vector<int>> nums = { { 1, 1, 0, 0 }, { -1, 1, 0, 0 }, { -1, 1, 0, 0 }, { -1, 1, 0, 0 } };
//vector<vector<int>> nums = { { 2, 2, 0, 1 }, { -1, 0, 0, -2 }, { 3, 0, 0, -2 }, { -1, 1, -2, 0 } };
for( int c1= 0; c1<4 ; ++c1 )
{
// transform
if (c1 != 0)
{
trans( nums[0] );
}
for( int c2= 0; c2<4 ; ++c2 )
{
if (c2 != 0)
{
trans(nums[1]);
}
for( int c3= 0; c3<4 ; ++c3 )
{
if (c3 != 0)
{
trans(nums[2]);
}
for (int c4 = 0; c4 < 4; ++c4)
{
if (c4 != 0)
{
trans(nums[3]);
}
if (isSquare(nums))
{
result = min( result, c1+c2+c3+c4 );
}
}
trans(nums[3]);
}
trans(nums[2]);
}
trans(nums[1]);
}
trans(nums[0]);
if (result == MOD)
result = -1;
printf("%d\n", result);
}
#else
vector<int> num = { 2,1,0,0 };
for (int i = 0; i < 4; i++)
{
trans( num );
}
vector<vector<int>> nums = { { 0, 2, 0, 0 }, { 0, -1, 0, 0 }, { 3, 2, 0, 0 }, { 3,-1, 0, 0 } };
cout << isS(nums) << endl;
#endif
system("pause");
return 0;
}