暴力code 60%
#include<iostream>
using namespace std;
const int N = 10010;
int st[N][N];//用二维数组表示某时刻收到的某订单
int w[N];//每家店的优先级
bool check[N];//状态
int n, m, t;
int main()
{
cin >> n >> m >> t;//店、订单、时间
for(int i = 1; i <= m; i++)
{
int ts, id;
cin >> ts >> id;//时刻,商店编号
st[id][ts] ++;
}
for(int i = 1; i <= n; i++)//枚举每家店
{
for(int j = 1; j <= t; j++)//枚举时间
{
if(st[i][j] > 0)
{
w[i] += 2 * st[i][j];
}
else
{
if(w[i] > 0) w[i] -= 1;
}
if(w[i] > 5) check[i] = true;
if(w[i] <= 3) check[i] = false;
}
}
int res = 0;
for(int i = 1; i <= n; i++)
{
if(check[i]) res ++;
}
cout << res << endl;
return 0;
}
AC优化code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int last[N], score[N];//记录上一个有订单的时刻,记录优先级
bool st[N];//记录是否符合条件
PII order[N];
int n, m, T;
int main()
{
scanf("%d%d%d", &n, &m, &T);
for(int i = 0; i < m; i++) scanf("%d%d", &order[i].x, &order[i].y);//输入
sort(order, order + m);//排序,pair排序是先按照第一个比较,第一个若是相同则按照第二个比较
//枚举每个订单
for(int i = 0; i < m; )
{
int j = i;
while(j < m && order[i] == order[j]) j ++;//可以判断同一时间同一店的重复情况
int t = order[i].x, id = order[i].y, cnt = j - i; //记录订单数量
i = j;
score[id] -= t - last[id] - 1;//是t时刻之前的(不包括t)
if(score[id] < 0) score[id] = 0;
if(score[id] <= 3) st[id] = false;
score[id] += cnt * 2;//t时刻
if(score[id] > 5) st[id] = true;
last[id] = t;
}
for(int i = 1; i <= n; i++)//枚举每家店,查看是否有T时刻之间无订单的
{
if(last[i] <= T)
{
score[i] -= T - last[i];
if(score[i] <= 3) st[i] = false;
}
}
int res = 0;
for(int i = 1; i <= n; i++)
{
res += st[i];
}
printf("%d", res);
return 0;
}