結果

問題 No.335 門松宝くじ
ユーザー cielciel
提出日時 2016-01-16 16:50:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 492 ms / 2,000 ms
コード長 1,767 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 61,028 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 00:08:55
合計ジャッジ時間 4,752 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 115 ms
4,348 KB
testcase_06 AC 176 ms
4,348 KB
testcase_07 AC 335 ms
4,348 KB
testcase_08 AC 295 ms
4,348 KB
testcase_09 AC 492 ms
4,348 KB
testcase_10 AC 484 ms
4,348 KB
testcase_11 AC 485 ms
4,348 KB
testcase_12 AC 479 ms
4,348 KB
testcase_13 AC 485 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:46:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   46 |         scanf("%d%d",&N,&M);
      |         ~~~~~^~~~~~~~~~~~~~
main.cpp:51:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   51 |                 for(int i=0;i<N;i++)scanf("%d",&v[i]),idx[v[i]]=i,rmq.update(i+1,v[i]);
      |                                     ~~~~~^~~~~~~~~~~~

ソースコード

diff #

#include <vector>
#include <map>
#include <cstdio>
using namespace std;
#define INF 9999999
class RMQ{
	int size;
	vector<int> minval;
	vector<int> maxval;
	pair<int,int> _query(int a,int b,int k,int l, int r){
		if(r<=a||b<=l)return make_pair(INF,-INF);
		if(a<=l&&r<=b)return make_pair(minval[k],maxval[k]);
		else{
			pair<int,int> vl=_query(a,b,k*2+1,l,(l+r)/2);
			pair<int,int> vr=_query(a,b,k*2+2,(l+r)/2,r);
			return make_pair(min(vl.first,vr.first),max(vl.second,vr.second));
		}
	}

public:
	RMQ(int k){init(k);}
	void init(int k){
		for(size=1;size<k;)size<<=1;
		//size=1<<(32-__builtin_clz(k));
		minval=vector<int>(2*size-1,INF);
		maxval=vector<int>(2*size-1,-INF);
	}

	void update(int k,int t){
		k+=size-1;
		minval[k]=maxval[k]=t;
		while(k>0){
			k=(k-1)/2;
			minval[k]=min(minval[k*2+1],minval[k*2+2]);
			maxval[k]=max(maxval[k*2+1],maxval[k*2+2]);
		}
	}

	pair<int,int> query(int a,int b){
		return _query(a,b,0,0,size);
	}
};

int main(){
	int N,M,R=0,r=0,r0,r1;
	scanf("%d%d",&N,&M);
	for(int m=0;m<M;m++){
		map<int,int>idx;
		vector<int>v(N);
		RMQ rmq(N+1);
		for(int i=0;i<N;i++)scanf("%d",&v[i]),idx[v[i]]=i,rmq.update(i+1,v[i]);
		r1=0;
		for(int a=1;a<=N;a++)for(int b=1;b<=N;b++){
			r0=0;
			int i=idx[a]+1,j=idx[b]+1,k=max(a,b);
			if(i<j){
				pair<int,int> x;
				if(1!=i){
					x=rmq.query(1,i);
					for(int e:{x.first,x.second})if((e-a)*(a-b)<0)r0=max(max(e,r0),k);
				}
				if(i+1!=j){
					x=rmq.query(i+1,j);
					for(int e:{x.first,x.second})if((a-e)*(e-b)<0)r0=max(max(e,r0),k);
				}
				if(j+1!=N+1){
					x=rmq.query(j+1,N+1);
					for(int e:{x.first,x.second})if((a-b)*(b-e)<0)r0=max(max(e,r0),k);
				}
				//printf("%d ",r0);
			}
			r1+=r0;
		}
		//printf("%d\n",r1);
		if(r<r1)r=r1,R=m;
	}
	printf("%d\n",R);
}
0