Cow XOR

题意:摘自NOCOW翻译(http://www.nocow.cn/index.php/Translate:USACO/cowxor

描述

农民约翰在喂奶牛的时候被另一个问题卡住了。他的所有N(1 <= N <= 100,000)个奶牛在他面前排成一行(按序号1..N的顺序),按照它们的社会等级排序。奶牛#1有最高的社会等级,奶牛#N最低。每个奶牛同时被指定了一个不唯一的附加值,这个数在0..2^21 - 1的范围内。

帮助农民约翰找出应该从哪一头奶牛开始喂,使得从这头奶牛开始的一个连续的子序列上,奶牛的附加值的异或最大。

如果有多个这样的子序列,选择结尾的奶牛社会等级最高的。如果还不唯一,选择最短的。

[编辑]格式

PROGRAM NAME: cowxor

INPUT FORMAT:

(file cowxor.in)

INPUT FORMAT

第1行:一个单独的整数N。

第2到N + 1行:N个0..2^21 - 1之间的整数,代表每头奶牛的被赋予的数。第j行描述了社会等级j - 1的奶牛。

OUTPUT FORMAT:

(file cowxor.out)

第 1 行: 3个空格隔开的整数,分别为:最大的异或值,序列的起始位置、终止位置。 时限0.5秒

[编辑]SAMPLE INPUT

5
1
0
5
4
2

[编辑]SAMPLE OUTPUT

6 4 5

[编辑]样例输出说明

最大异或值为6,从第4个开始喂,到第5个结束。

4 异或 2 = 6

(100) 异或 (010) = (110)

解题思路:

  1. 参考“brian007”的Trie解法

代码

/*
ID: zc.rene1
LANG: C
PROG: cowxor
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define SIZE 700000

typedef struct 
{
    int value;
    int index;
    int sub[2];
}node;

node nodes[SIZE];
int node_num;
int N;
int root;

int GetBit(int x, int index)
{
    return (x & (1 << index)) >> index;
}

void insert(int value, int index)
{
    int i, j, current;

    if (node_num == 0)
    {
	memset(nodes + node_num, -1, sizeof(node));
	node_num++;
	root = 0;
    }

    current = root;
    for (i=20; i>=0; i--)
    {
	j = GetBit(value, i);
	if (nodes[current].sub[j] == -1)
	{
	    memset(nodes + node_num, -1, sizeof(node));
	    nodes[current].sub[j] = node_num;
	    node_num++;
	}
	current = nodes[current].sub[j];
    }
    nodes[current].value = value;
    nodes[current].index = index;
}

int FindMaxIndex(int value)
{
    int i, j, current = root;

    for (i=20; i>=0 && current != -1; i--)
    {
	j = GetBit(value, i);
	if (nodes[current].sub[(j + 1) % 2] != -1)
	{
	    current = nodes[current].sub[(j + 1) % 2];
	}
	else
	{
	    current = nodes[current].sub[j];
	}
    }

    return current;
}

int main(void)
{
    FILE *fin, *fout;
    int i, j, num, result;
    int max = -1, max_i, max_j;

    fin = fopen("cowxor.in", "r");
    fout = fopen("cowxor.out", "w");

    node_num = 0;
    insert(0, 0);
    result = 0;

    fscanf(fin, "%d", &N);

    for (i=1; i<=N; i++)
    {
	fscanf(fin, "%d", &num);
	result ^= num;
	j = FindMaxIndex(result);
	if (j != -1 && (result ^ nodes[j].value) > max)
	{
	    max = result ^ nodes[j].value;
	    max_i = nodes[j].index + 1;
	    max_j = i;
	}
	insert(result, i);
    }

    fprintf(fout, "%d %d %d\n", max, max_i, max_j);

    return 0;
}




Line 58282: M00DD4C 08-28 09:41:12.701 891 891 I update_engine: [INFO:snapshot.cpp(2718)] Mapped COW device for system_ext_b at /dev/block/dm-38 Line 58283: M00DD4D 08-28 09:41:12.704 891 891 I update_engine: [INFO:writer_base.cpp(83)] COW image /dev/block/dm-38 has size 88068096 Line 58285: M00DD4F 08-28 09:41:12.729 891 891 I update_engine: [INFO:writer_v2.cpp(177)] Batch writes: enabled Line 58286: M00DD50 08-28 09:41:12.732 891 891 I update_engine: [INFO:writer_v2.cpp(182)] Not creating new threads for compression. Line 58287: M00DD51 08-28 09:41:12.733 891 891 I update_engine: [INFO:vabc_partition_writer.cc(254)] VABC XOR enabled for partition system_ext Line 58288: M00DD52 08-28 09:41:12.734 891 891 I update_engine: [INFO:vabc_partition_writer.cc(262)] Snapuserd supports XOR and merge sequence, writing merge sequence and delay writing COPY operations Line 58457: M00DDFB 08-28 09:41:16.188 891 891 W update_engine: [WARNING:verified_source_fd.cc(130)] Source hash from RAW device mismatched: found 784B25221EF7697C385AC7BAA5BCE6E4187FFE34B3A32BA69CC5176FA5CF6EB4, expected DC3041888E44BBDA8F960A514204634C03383A563324C6A165E4D06209C9EEC2 Line 65400: M00F86E 08-28 09:43:18.900 891 891 E update_engine: [ERROR:utils.cc(683)] fd_force_ro >= 0 failed: No such file or directory Line 65401: M00F86F 08-28 09:43:18.904 891 891 E update_engine: [ERROR:utils.cc(190)] rc >= 0 failed: Operation not permitted Line 65402: M00F870 08-28 09:43:18.905 891 891 E update_engine: [ERROR:extent_writer.cc(53)] utils::WriteAll(fd_, c_bytes + bytes_written, bytes_to_write) failed. Line 65403: M00F871 08-28 09:43:18.906 891 891 E update_engine: [ERROR:verified_source_fd.cc(80)] writer.Write(source_data.data(), source_data.size()) failed. Line 68848: E0105C5 08-28 09:43:50.088 1048 1087 I am_cpu : [891,0,update_engine,310785,75300,263960]解析log
08-29
------------------------ Super partition layout: ------------------------ super: 2048 .. 1918936: system_a (1916888 sectors) super: 1918976 .. 4295008: system_ext_a (2376032 sectors) super: 4296704 .. 5366512: vendor_a (1069808 sectors) super: 5367808 .. 5385752: product_a (17944 sectors) super: 5386240 .. 7901616: my_product_a (2515376 sectors) super: 7903232 .. 11739296: odm_a (3836064 sectors) super: 11741184 .. 11741840: my_engineering_a (656 sectors) super: 11743232 .. 11934000: vendor_dlkm_a (190768 sectors) super: 11935744 .. 11959992: system_dlkm_a (24248 sectors) super: 11960320 .. 11984144: system_dlkm_xts_a (23824 sectors) super: 12009472 .. 19036080: my_stock_a (7026608 sectors) super: 19036160 .. 19036816: my_heytap_a (656 sectors) super: 19038208 .. 19038864: my_carrier_a (656 sectors) super: 19040256 .. 19046648: my_region_a (6392 sectors) super: 19048448 .. 19049104: my_company_a (656 sectors) super: 19050496 .. 22247696: my_preload_a (3197200 sectors) super: 22249472 .. 22250128: my_bigball_a (656 sectors) super: 22251520 .. 22252576: my_manifest_a (1056 sectors) ------------------------ Block device table: ------------------------ Partition name: super First sector: 2048 Size: 13321109504 bytes Flags: none ------------------------ Group table: ------------------------ Name: default Maximum size: 0 bytes Flags: none ------------------------ Name: qti_dynamic_partitions_a Maximum size: 13316915200 bytes Flags: none ------------------------ Name: qti_dynamic_partitions_b Maximum size: 13316915200 bytes Flags: none ------------------------ --------------- Snapshot state: --------------- Update state: none Using snapuserd: 0 Using userspace snapshots: 0 Using io_uring: 0 Using o_direct: 0 Using skip_verification: 0 Cow op merge size (0 for uncapped): 0 Worker thread count: 0 Num verification threads: 0 Verify block size: 0 Using XOR compression: 1 Current slot: _a Boot indicator: booting from unknown slot Rollback indicator: No such file or directory Forward merge indicator: No such file or directory Source build fingerprint: 这个是lpdump的文件,现在解析会解析到 Maximum size: 13316915200 bytes这行,实际应该是 Size: 13321109504 bytes,如何修改
最新发布
11-18
T9-4 Find String 'C' 时间限制:1000ms 内存限制:128MB 文件名:T9-4.in T9-4.out Yuilice拥有着一个神奇的字符串s,该字符串只包含字符'C','O','W',字符串s的长度最高不超过2⋅10 5 。 你可以对于字符串进行以下两种操作: 选择两个相邻相等的字母并将其删除。 选择一个字母,将其替换为另外两个字母的任一排列。 现在Yuilice会给出Q(1≤Q≤2⋅10 5 )个s的子串,请你告诉他,他能否通过以上两种操作将给定的子串变为只包含单个字母'C'的字符串呢? 输入格式 第一行输入一个字符串s 第二行输入一个正整数 Q - 代表Yuilice给出的子串数量 以下 Q 行每行包含两个整数 l 和 r(1≤l≤r≤∣s∣,其中 ∣s∣ 表示 s 的长度) - 代表给出子串的在字符串s当中的范围。 输出格式 输出一个长为 Q 的字符串,如果第 i 个子串可以被转变则第 i 个字符为 'Y',否则为 'N'。 样例组 输入#1 复制 COW 6 1 1 1 2 1 3 2 2 2 3 3 3 输出#1 复制 YNNNYN 提示说明 【样例提示】 第1个子串本身已为符合题目条件的字符串,故为'Y'。 第5个子串可通过2步操作变为'C',即。 OW -> CWW -> C 这个样例字符串 COW 的其他子串均不能被转变为 'C'。 【数据范围】 数据 2-4 满足 ∣s∣≤5000 以及 Q≤5000。 数据 5-11 没有额外限制。 1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 ​ 5 int main() { 6 std::string filename = "example.txt"; 7 ​ 8 // 1. 写入文件 (ofstream) 9 { 10 std::ofstream out_file(filename); // 自动打开文件 11 ​ 12 if (!out_file.is_open()) { 13 std::cerr << "无法打开文件用于写入!" << std::endl; 14 return 1; 15 } 16 ​ 17 out_file << "Hello, World!\n"; 18 out_file << "This is written using C++ fstream.\n"; 19 ​ 20 // 文件将在 out_file 离开作用域时自动关闭(析构函数调用) 21 } // ✅ 此处自动调用析构函数,close() 被隐式执行 22 ​ 23 std::cout << "数据已写入文件。\n"; 24 ​ 25 // 2. 读取文件 (ifstream) 26 { 27 std::ifstream in_file(filename); 28 ​ 29 if (!in_file.is_open()) { 30 std::cerr << "无法打开文件用于读取!" << std::endl; 31 return 1; 32 } 33 ​ 34 std::string line; 35 std::cout << "从文件中读取的内容:\n"; 36 while (std::getline(in_file, line)) { 37 std::cout << line << '\n'; 38 } 39 } 40 ​ 41 return 0; 42 } 43 ​ 输 入 输 出 c++ 文件输入输出加fclose
10-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值