1 / 3
Jul 2011

Hi every body, my first post here smile
I'm getting a WA response from the judge system for the problem code GERGOVIA
http://www.spoj.pl/problems/GERGOVIA1
and here is my code:

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <math.h>
using namespace std;
int main() {
	while (true) {
		int n, units = 0;
		cin >> n;
		if (n == 0)
			break;
		// first: index, second: value
		vector<pair<int, int> > sellers, consumers;
		for (int i = 0; i < n; i++) {
			int person;
			cin >> person;
			// Seller
			if (person < 0) {
				sellers.push_back(make_pair(i, -person));
				bool stop = false;
				int sellInd = sellers.size() - 1;
				while (!stop && !consumers.empty()) {
					int consInd = consumers.size() - 1;
					if (sellers[sellInd].second < consumers[consInd].second) {
						units += (sellers[sellInd].second * abs(
								i - consumers[consInd].first));
						consumers[consInd].second -= sellers[sellInd].second;
						sellers.pop_back();
						stop = true;
					} else if (sellers[sellInd].second
							> consumers[consInd].second) {
						units += (consumers[consInd].second * abs(
								i - consumers[consInd].first));
						sellers[sellInd].second -= consumers[consInd].second;
						consumers.pop_back();
					} else {
						units += (consumers[consInd].second * abs(
								i - consumers[consInd].first));
						consumers.pop_back();
						sellers.pop_back();
						stop = true;
					}
				}
			}
			// Consumer
			else if (person > 0) {
				consumers.push_back(make_pair(i, person));
				bool stop = false;
				int consInd = consumers.size() - 1;
				while (!stop && !sellers.empty()) {
					int sellInd = sellers.size() - 1;
					if (sellers[sellInd].second < consumers[consInd].second) {
						units += (sellers[sellInd].second * abs(
								i - sellers[sellInd].first));
						consumers[consInd].second -= sellers[sellInd].second;
						sellers.pop_back();
					} else if (sellers[sellInd].second
							> consumers[consInd].second) {
						units += (consumers[consInd].second * abs(
								i - sellers[sellInd].first));
						sellers[sellInd].second -= consumers[consInd].second;
						consumers.pop_back();
						stop = true;
					} else {
						units += (consumers[consInd].second * abs(
								i - sellers[sellInd].first));
						consumers.pop_back();
						sellers.pop_back();
						stop = true;
					}
				}
			}
		}
		cout << units << endl;
	}
	return 0;
}

i wish if any one could provide me some test cases to know where exactly I'm wrong

  • created

    Jul '11
  • last reply

    Jul '11
  • 2

    replies

  • 230

    views

  • 1

    user

  • 1

    link

OK it's finally accepted, just if some one wanted to know, the problem that the result should be of type long long, read constraints well later smiley

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 21 13d

Want to read more? Browse other topics in C and C++ or view latest topics.