文章目录
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;
}