我不是猪:第一题:
#include <iostream>
#include <vector>
using namespace std;
int numOfplan();
int main()
{
cout << numOfplan() << endl;
return 0;
}
int numOfplan()
{
int n;
cin >> n;
vector<int> arr(n);
int sum = 0;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
sum += arr[i];
}
if (sum % arr.size() != 0)
return -1;
int avg = sum / arr.size();
int iCount = 0;
for (unsigned int i = 0; i < arr.size(); i++)
{
if ((arr[i] - avg) % 2 == 0)
iCount += (arr[i] - avg) > 0 ? (arr[i] - avg) / 2 : (avg - arr[i]) / 2;
else
return -1;
}
return iCount & 1 ? -1 : iCount / 2;
}
第二题:
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
long long _maxWastage1(long long h); // 方法1
long long _maxWastage2(long long h); // 方法2
int main()
{
// 由于 10^18 在 long long 的表示范围内
long long h;
cin >> h;
cout << _maxWastage2(h) << endl;
return 0;
}
long long _maxWastage1(long long h)
{
long long result = sqrt(h);
for (long long i = result - 1; i >= 0; i--)
{
if (i + i * i < h)
return i;
}
return 0;
}
long long _maxWastage2(long long h)
{
long long left = 0;
long long right = h;
long long mid = 0;
while (left < right)
{
mid = (right + left + 1) >> 1;
if (mid + mid * mid > h)
right = mid - 1;
else
left = mid;
}
return left;
}
第三题:
#include <iostream>
using namespace std;
bool is_ok_(int _m);
bool is_ok(int l, int r, int _m);
#define MAXN (500+10)
#define MAXM (500+10)
#define MAXT (2000000+10)
int a[MAXN][MAXM], sum[MAXN][MAXM] = { 0 };
int n, m;
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> a[i][j];
sum[i][j] = sum[i - 1][j] + a[i][j];
}
}
int left = 1, right = 1, ans = 0;
for (int j = 1; j <= m; j++)
right += sum[n][j];
for (int i = 1; i <= 60; i++)
{
int mid = (left + right) / 2;
if (is_ok_(mid))
{
left = ans = mid;
}
else
right = mid;
}
cout << ans << endl;
return 0;
}
bool is_ok_(int _m)
{
int p = 0, l = 1;
for (int i = 1; i <= n; i++)
{
if (is_ok(l, i, _m))
{
l = i + 1; p++;
}
}
if (p >= 4)
return 1;
else
return 0;
}
bool is_ok(int l, int r, int _m)
{
int tot = 0, p = 0;
for (int i = 1; i <= m; i++)
{
tot += sum[r][i] - sum[l - 1][i];
if (tot >= _m)
{
tot = 0;
p++;
}
}
if (p >= 4)
return 1;
else
return 0;
}
投递网易等公司10个岗位 >
0 点赞 评论 收藏
分享
关注他的用户也关注了: