【基础二分+前缀和】

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值