1227. 分巧克力
#include <bits/stdc++.h>
#define endl "\n"
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
using namespace std;
const int N = 2e5 + 10, mod = 1e9+7;
typedef pair<int, int> PII;
typedef long long ll;
int a[N][2];
int n, k;
bool check(int x) { // 看能不能够分得出k块
int cnt = 0, i;
if(x == 0) return true;
for (i = 1; i <= n; i ++ ) {
cnt += (a[i][0]/x) * (a[i][1]/x);
if(cnt >= k) return true;
}
return false;
}
void run() {
cin >>n >>k;
for (int i = 1; i <= n; i ++) {
cin >> a[i][0] >> a[i][1];
}
int l = 0, r = 100010;
while(l <= r) { // 假设一个人可以分到x的边长
int mid = (l + r) >> 1;
if(check(mid)) l = mid + 1;
else r = mid - 1;
}
cout << r <<endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
//cin >> T;
while(T -- ) run();
return 0;
}
1221. 四平方和
wac,竟然可以过11个数据,看来卡得并不严格
#include <bits/stdc++.h>
#define endl "\n"
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
using namespace std;
const int N = 2e5 + 10, mod = 1e9+7;
typedef pair<int, int> PII;
typedef long long ll;
void run() {
int n; cin >> n;
int i, j, k;
for (i = 0; i <= 2500; i ++ ) {
for (j = i; j <= 2500; j ++) {
for (k = j; k <= 2500; k ++) {
int d = n- i*i - j*j - k*k;
int t = (int)sqrt(d);
if(t*t == d) {
cout <<i << ' ' << j <<' ' <<k <<' ' << t <<endl;
return ;
}
}
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
//cin >> T;
while(T -- ) run();
return 0;
}
这个代码一直错,已经找到原因了
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 5e6 + 10;
int mp[N];
int main() {
int n; cin >> n;
int a, b, c, d;
for (c = 0; c <= 2300; c ++ ) {
for (d = c; d <= 2300; d ++ ) { // 枚举的问题,超过了数组的最大承受范围
int t = c*c + d*d;
if(mp[t] == 0) {
mp[t] = c;
}
}
}
for (a = 0; a <= 2300; a ++ ) {
for (b = a; b <= 2300; b ++ ) {
int t = n - a*a - b*b;
if(mp[t] == 0) continue;
else {
int k = t - mp[t]*mp[t];
k = sqrt(k);
cout << a <<' ' << b <<' ' <<mp[t] << ' ' <<k << endl;
return 0;
}
}
}
}
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 5e6 + 10;
int mp[N];
int main()
{
int n; cin >> n;
memset(mp, -1, sizeof mp);
for (int c = 0; c*c <= n; c ++) {
for (int d = c; c*c + d*d <= n; d ++ ) {
int t = c*c + d*d;
if(mp[t] == -1) mp[t] = c; // mp[0] = 0, 但是之后会跳过这个数导致错失这个答案
}
}
for (int a = 0; a*a <= n; a ++ ) {
for (int b = a; a*a + b*b <= n; b ++ ) {
int t = n - a*a - b*b;
if(mp[t] == -1) continue;
int c = mp[t];
int d = sqrt(t - c*c);
cout << a <<' ' << b << ' ' << c << ' ' << d <<endl;
return 0;
}
}
}
99. 激光炸弹
原来一个地方可以有多个物品,题目都理解错了,还能过12个点,数据真的水
#include <iostream>
#include <cstring>
#include <algorithm>
#define deb3(x, y, z) cout << #x << "=" << x << ", " << #y << "=" << y <<", "<< #z << "=" << z << endl
using namespace std;
const int N = 5010;
typedef long long ll;
int s[N][N], ans;
int main()
{
int n1, r; cin >> n1 >> r;
r = min(r, 5001);
int i, j, n = r, m = r;
for (i = 0; i < n1; i ++ ) {
int x, y, w; cin >> x >> y >> w;
x += 1, y += 1, // 注意下标又从0开始的
s[x][y] = max(s[x][y], w); // 注意取最大值
n = max(n, x), m = max(m, y); // 找到最远的点,以此点作为前缀和数组最后一点
}
for (i = 1; i <= n; i ++ ) {
for (j = 1; j <= m; j ++ ) {
s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + s[i][j]; // 笑麻了,还有他自己
// cout << s[i][j] << ' ';
}
// cout << endl;
}
for (i = r; i <= n; i ++ ) {
for (j = r; j <= m; j ++ ) {
int t = s[i][j] - s[i-r][j] - s[i][j-r] + s[i-r][j-r];
ans = max(ans, t) ;
}
}
cout << ans << endl;
}
#include <iostream>
#include <cstring>
#include <algorithm>
#define deb3(x, y, z) cout << #x << "=" << x << ", " << #y << "=" << y <<", "<< #z << "=" << z << endl
using namespace std;
const int N = 5010;
typedef long long ll;
int s[N][N], ans;
int main()
{
int n1, r; cin >> n1 >> r;
r = min(r, 5001);
int i, j, n = r, m = r;
for (i = 0; i < n1; i ++ ) {
int x, y, w; cin >> x >> y >> w;
x += 1, y += 1, // 注意下标又从0开始的
s[x][y] += w; // 注意取最大值
n = max(n, x), m = max(m, y); // 找到最远的点,以此点作为前缀和数组最后一点
}
for (i = 1; i <= n; i ++ ) {
for (j = 1; j <= m; j ++ ) {
s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + s[i][j]; // 笑麻了,还有他自己
// cout << s[i][j] << ' ';
}
// cout << endl;
}
for (i = r; i <= n; i ++ ) {
for (j = r; j <= m; j ++ ) {
int t = s[i][j] - s[i-r][j] - s[i][j-r] + s[i-r][j-r];
ans = max(ans, t) ;
}
}
cout << ans << endl;
}