CSP认证-邻域均值、DHCP服务器

本文介绍了CSP认证中的两个主题:一是如何利用二维前缀和实现邻域均值计算;二是探讨了DHCP服务器如何管理和分配IP池,满足特定需求。
摘要由CSDN通过智能技术生成

T2-邻域均值

维护二维前缀和

#pragma gcc optimize(2)
#include <bits/stdc++.h>
using namespace std;

const int N = 610;
int pic[N][N], s[N][N];
int n, L, r, t;

bool judge(int x, int y)
{
    int x1, y1, x2, y2;
    x1 = max(1, x - r), x2 = min(n, x + r);
    y1 = max(1, y - r), y2 = min(n, y + r);
    int sum = s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
    int num = (y2 - y1 + 1) * (x2 - x1 + 1); 
    if(sum <= t * num) return true;
    else return false;
}

int main()
{
    scanf("%d%d%d%d", &n, &L, &r, &t);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            scanf("%d", &pic[i][j]);
            // 维护二维前缀和
            s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + pic[i][j];
        }
    
    int cnt = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            if(judge(i, j)) cnt++;
    printf("%d\n", cnt);
    return 0;
}

T3-DHCP服务器

维护IP池的相关信息,按照题目的要求即可实现

#pragma gcc optimize(2)
#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int n, tdef, tmax, tmin, nn;
string h;
struct IP
{
    int state; // 0未分配、1待分配、2占用、3过期
    int te;
    string owner;
}ip[N];

void updata_ip_state(int t_cur)
{
    for(int i = 1; i <= n; i++)
    {
        // 易写错点
        if(ip[i].te && ip[i].te <= t_cur) 
        {
            if(ip[i].state == 1) ip[i].state = 0, ip[i].owner = "", ip[i].te = 0;
            else ip[i].state = 3, ip[i].te = 0;
        }
    }
}

int get_ip_by_state(int state)
{
    for(int i = 1; i <= n; i++)
    {
        if(ip[i].state == state)
            return i;
    }
    return 0;
}


int main()
{
    // freopen("a.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    
    cin >> n >> tdef >> tmax >> tmin >> h >> nn;
    while(nn--)
    {
        int tc;
        string client, server, type;
        int id, te; 
        cin >> tc >> client >> server >> type >> id >> te;
        if(server != h && server != "*" && type != "REQ") continue;
        if(type != "DIS" && type != "REQ") continue;
        if(server == "*" && type != "DIS" || server == h && type == "DIS") continue;

        updata_ip_state(tc);
        if(type == "DIS")
        {
            int res = 0;
            for(int i = 1; i <= n; i++)
            {
                if(ip[i].owner == client){
                    res = i;
                    break;
                }
            }
            if(!res) res = get_ip_by_state(0);
            if(!res) res = get_ip_by_state(3);
            if(!res) continue;

            ip[res].state = 1;
            ip[res].owner = client;
            if(te == 0) ip[res].te = tc + tdef;
            else {
                int tt = te - tc;
                tt = max(tt, tmin), tt = min(tt, tmax);
                ip[res].te = tc + tt;
            }
            
            cout << h <<" "<< client << " OFR " << res <<" "<< ip[res].te<<'\n';

        }
        
        else if(type == "REQ"){
            if(server != h) {
                for(int i = 1; i <= n; i++)
                {
                    if(ip[i].owner == client && ip[i].state == 1) 
                        ip[i].state = 0, ip[i].owner = "", ip[i].te = 0;
                }
            }
            else{
                if(!(id >= 1 && id <= n && ip[id].owner == client))
                    cout << h <<" "<< client << " NAK " << id <<" "<< 0 <<'\n';
                
                else{
                    ip[id].state = 2;

                    if(te == 0) ip[id].te = tc + tdef;
                    else {
                        int tt = te - tc;
                        tt = max(tt, tmin), tt = min(tt, tmax);
                        ip[id].te = tc + tt;
                    }

                    cout << h <<" "<< client << " ACK " << id <<" "<< ip[id].te <<'\n';
                }

            }
        }
    }

    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值