/ Randle /

记录详情

Time Exceeded


  
# 状态 耗时 内存占用
#1 Accepted 9ms 10.328 MiB
#2 Accepted 10ms 10.359 MiB
#3 Accepted 5ms 10.332 MiB
#4 Accepted 8ms 12.598 MiB
#5 Accepted 58ms 22.996 MiB
#6 Accepted 74ms 23.598 MiB
#7 Time Exceeded ≥1007ms ≥71.469 MiB
#8 Time Exceeded ≥1006ms ≥80.875 MiB
#9 Time Exceeded ≥1000ms ≥73.473 MiB
#10 Time Exceeded ≥1006ms ≥67.121 MiB

代码

#include <stdio.h>
#include <iostream>
#define inf 1e9
using namespace std;
inline int read() {
	int k=0;
	char f=1;
	char c=getchar();
	for(; !isdigit(c); c=getchar() )
		if(c=='-')
			f=-1;
	for(; isdigit(c); c=getchar() )
		k=k*10+c-'0';
	return k*f;
}
int n,m,t;
int s[5002][1002],a[5002][1002],ans[1002],Max[5002][1002],Min[5002][1002],MaxID[5002][1002],MinID[5002][1002],Q[1002];

void get_min(int line) {
	int head,tail,i;
	head=1;
	tail=1;
	Q[tail]=1;
	Min[line][1]=s[line][0];
	for (i=1; i<=n; i++) {
		while ((head<=tail)&&(s[line][i]<s[line][Q[tail]]))//删除队尾元素
			tail--;                            //插入队尾元素
		Q[++tail]=i;
		while ((head<=tail)&&(Q[head]<i-t+1)) //删除队首过时元素
			head++;
		/*由于每一层循环只添加一个数,因此每次最多删除一次队首,
		故可改成if语句*/
		Min[line][i]=s[line][Q[head]];
		MinID[line][i] = Q[head];
	}
}
void get_max(int line) {
	int head,tail,i;
	head=1;
	tail=1;
	Q[tail]=1;
	Max[line][1]=s[line][0];
	for (i=1; i<=n; i++) {
		while ((head<=tail)&&(s[line][i]>s[line][Q[tail]]))//删除队尾元素
			tail--;                            //插入队尾元素
		Q[++tail]=i;
		while ((head<=tail)&&(Q[head]<i-t+1)) //删除队首过时元素
			head++;
		/*由于每一层循环只添加一个数,因此每次最多删除一次队首,
		故可改成if语句*/
		Max[line][i]=s[line][Q[head]];
		MaxID[line][i] = Q[head];
	}
}
void solve(int line) {
	int minid = 0,maxid = 0;
	get_max(line);
	get_min(line);
	int maxx = 0;
	for(int i = 1;i<=t-1;i++){
		for(int j = 1;j<=i;j++){
			if(s[line][i]-s[line][j-1]>maxx){
				maxx = s[line][i] - s[line][j-1];
				minid = j;
				maxid = i;
			}
		}
	}
	for(int i = 1; i<=n; i++) {
		if((!maxid&&!minid)||(Max[line][i]-Min[line][i]>maxx&&MaxID[line][i]-MinID[line][i]>0)) {
			maxid = MaxID[line][i];
			minid = MinID[line][i]+1;
			maxx = Max[line][i] - Min[line][i];
		} else if(Max[line][i]-Min[line][i]==maxx&&MaxID[line][i]-MinID[line][i]>maxid-minid) {
			maxid = MaxID[line][i];
			minid = MinID[line][i]+1;
			maxx = Max[line][i] - Min[line][i];
		}
	}
	if(minid==0&&maxid==0)return;
	for(int i = minid; i<=maxid; i++) {
		ans[i]++;
	}
}
int main() {
	n =read();
	m = read();
	t = read();
	t++;
	for(int i = 1; i<=m; i++) {
		for(int j = 1; j<=n; j++) {
			a[i][j] = read();
			s[i][j] = s[i][j-1]+a[i][j];
		}
	}
	for(int i = 1; i<=m; i++) solve(i);
	for(int i = 1; i<=n; i++) cout<<ans[i]<<" ";
	return 0;
}

信息

递交者
类型
递交
题目
骂战(原创)
题目数据
下载
语言
C++
递交时间
2017-10-12 20:25:55
评测时间
2017-10-12 20:25:55
评测机
分数
60
总耗时
≥4189ms
峰值内存
≥80.875 MiB