codechef - Discrepancies in the Voters List 题解

codechef的本题算法也不难,但是codechef喜欢大数据,动不动就过万过十万,输入输出处理不好就会超时。

就像本题最大数据可能达到15万个整数。普通输入输出铁定超时了。

这里使用fread和fwrite这两个函数,设置好buffer,速度还是相当快的,而且相对很多程序都比较简单的了。

主要注意:

每个buffer数据块和下一个buffer数据块之间的衔接,不能破坏了最终需要的格式,也不要在输入的时候破坏了数据连续性。

很低层的东西了,不过用多久慢慢熟悉了。

题意就是:找出三个数列中的所有在两个数列都出现的数值。

原题:http://www.codechef.com/problems/VOTERS/

处理算法的代码还不到输入输出代码的一半呢。

#pragma once

#include <utility>
#include <algorithm>
#include <assert.h>
#include <string>
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;

int DiscrepanciesintheVotersList()
{
	int N1, N2, N3, n = 0;
	scanf("%d %d %d\n", &N1, &N2, &N3);
	int *A = new int[N1];
	int *B = new int[N2];
	int *C = new int[N3];

	char buffer[10000];
	int num = 0, id = 0, i = 0, j = 0, k = 0;
	while ((n = fread(buffer, 1, 10000, stdin)) > 0)
	{
		for (int i = 0; i < n; i++)
		{
			if (‘0‘ <= buffer[i] && buffer[i] <= ‘9‘)//避免有空格
			{
				num = num * 10 + (buffer[i] - ‘0‘);
			}
			else if (buffer[i] == ‘\n‘)
			{
				if (j < N1) A[j] = num;
				else if (j < N1+N2) B[j-N1] = num;
				else C[j-N1-N2] = num;
				j++;
				num = 0;
			}
		}
	}
	if (0 != num) C[N3-1] = num;//需要额外处理一个数据,不能在循环中处理

	int newN = max(N1, max(N2, N3));
	int *D = new int[newN];

	id = 0, i = 0, j = 0, k = 0;
	while (i < N1 || j < N2 || k < N3)
	{
		int t = (1<<29);
		if (i < N1) t = min(t, A[i]);
		if (j < N2) t = min(t, B[j]);
		if (k < N3) t = min(t, C[k]);
		int c = 0;
		if (i < N1 && t == A[i]) c++, i++;
		if (j < N2 && t == B[j]) c++, j++;
		if (k < N3 && t == C[k]) c++, k++;
		if (c > 1) D[id++] = t;
	}

	printf("%d\n", id);
	for (int i = 0; i < id; )
	{
		int j = 0;
		while (j < 9500 && i < id)//这里j要小于最大buffer 10000
		{
			int sum = 0, tmp = D[i], st = j;
			while (tmp)
			{
				sum = tmp % 10;
				tmp /= 10;
				buffer[j++] = sum + ‘0‘;
			}
			reverse(buffer+st, buffer+j);
			buffer[j++] = ‘\n‘;
			i++;
		}
		fwrite(buffer, 1, j, stdout);
		//每次都需要多打个‘\n‘,因为数据可能大于当次的buffer
	}

	delete []A;
	delete []B;
	delete []C;
	delete []D;
	return 0;
}

codechef - Discrepancies in the Voters List 题解

时间: 2024-11-05 12:23:14

codechef - Discrepancies in the Voters List 题解的相关文章

codechef Chef and The Right Triangles 题解

Chef and The Right Triangles The Chef is given a list of N triangles. Each triangle is identfied by the coordinates of its three corners in the 2-D cartesian plane. His job is to figure out how many of the given triangles are right triangles. A right

Codechef July Challenge 2014部分题解

Dish Owner(并查集) 链接:http://www.codechef.com/JULY14/problems/DISHOWN/ 题意分析:本题主要操作就是给出0 x y时,拥有第x道菜的厨师与拥有第y道菜的厨师pk,谁拥有的所有菜的其中一道菜(不一定是x或y)的分值比较高谁就获胜,并赢得loser的所有菜.即比较的是每个人分值最高的菜,所以对于非loser的人来说,他的分值最高的菜是不变的.综合题意用并查集易解. #include <cstdio> const int Maxn=100

codechef Row and Column Operations 题解

You are given an N × N grid initially filled by zeros. Let the rows and columns of the grid be numbered from1 to N, inclusive. There are two types of operations can be applied to the grid: RowAdd R X: all numbers in the row R should be increased by X

Codechef Nuclear Reactors 题解

There are K nuclear reactor chambers labelled from 0 to K-1. Particles are bombarded onto chamber 0. The particles keep collecting in the chamber 0. However if at any time, there are more than N particles in a chamber, a reaction will cause 1 particl

codechef Cleaning Up 题解

After a long and successful day of preparing food for the banquet, it is time to clean up. There is a list of n jobs to do before the kitchen can be closed for the night. These jobs are indexed from 1 to n. Most of the cooks have already left and onl

codechef Permutation Cycles 题解

We consider permutations of the numbers 1,..., N for some N. By permutation we mean a rearrangment of the number 1,...,N. For example 2  4  5  1  7  6  3  8 is a permutation of 1,2,...,8. Of course, 1  2  3  4  5  6  7  8 is also a permutation of 1,2

codechef Turbo Sort 题解

Input t – the number of numbers in list, then t lines follow [t <= 10^6]. Each line contains one integer: N [0 <= N <= 10^6] Output Output given numbers in non decreasing order. Example Input: 5 5 3 6 7 1 Output: 1 3 5 6 7 大数据的排序,输入和输出. 一开始使用了cou

codechef Top Batsmen题解

A cricket team consists of 11 players and some are good at batting, others are good at bowling and some of them are good at both batting and bowling. The batting coach wants to select exactly K players having maximum possible sum of scores. Given the

Codechef Maximum Weight Difference题解

Maximum Weight Difference Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants hi