这题洛谷数据输入格式有误
查看原帖
这题洛谷数据输入格式有误
554746
yiming564楼主2023/11/22 23:24

先贴代码

#include <bits/stdc++.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>

int n, m;

char op[200000];
int t[200000];

int calculate(int start, int mask)
{
	for (int i = 0; i < n; i++)
	{
		switch (op[i])
		{
		case 'A':
			start &= t[i];
			break;
		
		case 'O':
			start |= t[i];
			break;

		case 'X':
			start ^= t[i];
			break;

		default:
			break;
		}
	}
	return start & mask;
}

char *input;
char *end;

void read_data()
{
	while (*input != ' ')
	{
		n *= 10;
		n += *input - '0';
		input++;
	}
	input++;

	while (*input != '\n')
	{
		m *= 10;
		m += *input - '0';
		input++;
	}
	input++;
	
	for (int i = 0; i < n - 1; i++)
	{
		op[i] = *input;
		while (*input != ' ')
			input++;
		input++;
		while (*input != '\n')
		{
			t[i] *= 10;
			t[i] += *input - '0';
			input++;
		}
		input++;
	}

	op[n - 1] = *input;
	while (*input != ' ')
		input++;
	input++;
	while (input != end)
	{
		t[n - 1] *= 10;
		t[n - 1] += *input - '0';
		input++;
	}
	input++;
}

int main()
{
	int fd = open64("/dev/stdin", O_RDONLY);
	struct stat64 buf;
	uint64_t length = fstat64(fd, &buf);
	input = (char *)mmap64(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	close(fd);
	end = input + buf.st_size;
	read_data();
	int ans = 0;
	int my_ans = 0;
	int i = 1;
	for (; true; i++)
	{
		int base = 1 << i - 1;
		int false_value = calculate(0, base);
		int true_value = calculate(base, base);
		
		if (false_value < true_value)
		{
			my_ans += base;
			if (my_ans > m)
				break;
			ans += true_value;
		}
		else
		{			
			ans += false_value;
			if (i - 1 > std::__lg(m))
				break;
		}
	}
	while (i <= 31)
	{
		i++;
		ans += calculate(0, 1 << i - 1);
	}
	
	printf("%d", ans);
	munmap(input, buf.st_size);
	return 0;
}

根据数据输入的格式,输入的最后一行应该没有换行符,这种情况下代码不应该有误,而实测洛谷数据输入的最后一行自动添加了换行符,导致使用快读全部WA

2023/11/22 23:24
加载中...