Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register
北京大学《ACM/ICPC大学生程序设计竞赛训练》暑期课面向全球招生!

c++ 47ms g++WA可以帮我找下原因吗

Posted by 980306 at 2018-03-16 12:27:41 on Problem 2549
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<iomanip>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<string.h>
#include<set>
#include<stack>
#include<map>
using namespace std;
#pragma warning(disable:4996)

#define rep(i, n) for(int i=0; i<n ;i++)
#define Rep(i, n) for(int i=1; i<=n; i++)
#define SYS system("pause");
typedef long long ll;
const int INF = 0x7fffffff;
const int MAX_N = 1e3 + 5;
const int MAX_V = 0;
const int MAX_M = 0;
const int MAX_Q = 0;

const int mod = 1e6 + 5;
int N;
int a[MAX_N];
int head[mod + 5];
int cnt = 0;

struct Node {
	int x, y, nxt;
}nd[MAX_N*MAX_N];


void add_node(int x, int y) {
	nd[cnt].x = x; nd[cnt].y = y;
	int key = abs(x + y) % mod;
	nd[cnt].nxt = head[key];
	head[key] = cnt++;
}

bool search_node(int x, int y) {
	int key = abs(x - y) % mod;
	for (int i = head[key]; i != -1; i = nd[i].nxt) {
		if (nd[i].x != x && nd[i].y != y && nd[i].x != y && nd[i].y != x) {
			if(x-y==nd[i].x+nd[i].y)
				return true;
		}
	}
	return false;
}

int main() {
	ios::sync_with_stdio(0);
	while (cin >> N && N) {
		cnt = 0;
		memset(head, -1, sizeof head);
		rep(i, N) {
			cin >> a[i];
		}
		sort(a, a + N);
		Rep(i, N - 1) {
			rep(j, i) {
				add_node(a[i], a[j]);
			}
		}
		for (int i = N - 1; i >= 0; i--) {
			for (int j = 0; j < N; j++) {
				if (i != j && search_node(a[i], a[j])) {
					cout << a[i] << endl;
					goto ans;
				}
			}
		}
		puts("no solution");
		ans:;
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator