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

错误的代码也能通过!!!

Posted by cky050 at 2015-12-12 16:26:55 on Problem 2010
3 4 200
1 1
2 2
3 201
4 4


-----------------------

#include <iostream>
#include <algorithm>
using namespace std;
#define MAX_COW 100000 + 100

int N, C, F;
int lower[MAX_COW], upper[MAX_COW];
struct Cow
{
	int rank_by_score, score, aid;
};
Cow cow_score[MAX_COW], cow_aid[MAX_COW];
bool less_by_score(Cow& a, Cow& b)
{
	return a.score < b.score;
}
bool less_by_aid(Cow& a, Cow& b)
{
	return a.aid < b.aid;
}

///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> N >> C >> F;
	int half = N / 2;
	for (int i = 0; i < C; ++i)
	{
		cin >> cow_score[i].score >> cow_score[i].aid;   // 分数  学费
	}
	sort(cow_score, cow_score + C, less_by_score);
	for (int i = 0; i < C; ++i)
	{
		cow_score[i].rank_by_score = i;
	}
	memcpy(cow_aid, cow_score, sizeof(Cow) * C);
	sort(cow_aid, cow_aid + C, less_by_aid);

	int lb = 0, ub = C, ans = -1;
	while (ub - lb > 1)
	{
		int mid = (ub + lb) >> 1;
		// 将牛分为左右两个区间,统计左区间符合条件的数目left和右区间的数目right
		// 情况比较多,总体是二分,实际上有四种情况
		int left = 0, right = 0, total_aid = cow_score[mid].aid;
		for (int i = 0; i < C; ++i)
		{
			if ((cow_aid[i].rank_by_score < mid) && (total_aid + cow_aid[i].aid <= F) && (left < N / 2))
			{
				total_aid += cow_aid[i].aid;
				++left;
			}
			else if ((cow_aid[i].rank_by_score > mid) && (total_aid + cow_aid[i].aid <= F) && (right < N / 2))
			{
				total_aid += cow_aid[i].aid;
				++right;
			}
		}
		// 四种情况
		if ((left < N / 2) && (right < N / 2))
		{
			ans = -1;
			break;
		}
		else if (left < N / 2)
		{
			lb = mid;
		}
		else if (right < N / 2)
		{
			ub = mid;
		}
		else
		{
			ans = cow_score[mid].score;
			lb = mid;
		}
	}
	cout << ans << endl;

	return 0;
}


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