题目链接:点击查看

题目大意:初始时给出一个长度为 nnn 的序列,每次可以询问 kkk 个位置的异或和,现在需要以最少的询问获得整个序列的异或和

题目分析:因为是异或,我们只关心每个位置被询问的次数是奇数还是偶数,不妨设置一个集合,将询问次数为奇数次数的位置都放进来

那么我们初始时的状态是“集合大小为 000”,目标状态是“集合大小为 nnn”,可以通过 bfsbfsbfs 去扩展,对于某个状态 xxx 来说,假设我们需要选择 jjj 个集合内的数,那么自然需要选择 k−jk-jk−j 个集合外的数,集合的最终大小会变成 x−j+(k−j)=x+k−2∗jx-j+(k-j)=x+k-2*jx−j+(k−j)=x+k−2∗j,因为 nnn 比较小,所以可以用 bfsbfsbfs 在 O(nk)O(nk)O(nk) 的时间复杂度内处理出来

然后就是还原路径的过程了,在 bfsbfsbfs 的过程中记录一下转移的路径,然后正向去模拟即可。虽然话是这么说,但是写起来确实有些难度,在代码中加入了不少注释,可以直接参考代码

代码:

// Problem: E. Lost Array
// Contest: Codeforces - Codeforces LATOKEN Round 1 (Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/1534/problem/E
// Memory Limit: 256 MB
// Time Limit: 1500 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
int n,k,d[N],pre[N];
bool vis[N];
int query(vector<int>node) {printf("?");for(auto it:node) {printf(" %d",it);}puts("");fflush(stdout);int ans;scanf("%d",&ans);return ans;
}
void bfs() {memset(d,-1,sizeof(d));queue<int>q;d[0]=0;pre[0]=-1;q.push(0);while(q.size()) {int u=q.front();q.pop();for(int i=0;i<=k;i++) {//选择i个集合内的数字和(k-i)个集合外的数字if(i>u||k-i>n-u) {continue;}int v=u-i+(k-i);if(d[v]!=-1) {continue;}d[v]=d[u]+1;pre[v]=u;q.push(v);}}
}
int main()
{#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);scanf("%d%d",&n,&k);bfs();if(d[n]==-1) {return 0*puts("-1");}vector<int>node;int pos=n;while(pos!=-1) {node.push_back(pos);pos=pre[pos];}reverse(node.begin(),node.end());int ans=0;for(int i=1;i<(int)node.size();i++) {//每次集合的大小需要从node[i-1]变成node[i]//有y=x-delta+(k-delta)=x+k-2*delta,可以计算出delta=(x+k-y)/2int delta=(node[i-1]+k-node[i])/2;vector<int>num;int c0=delta,c1=k-delta;//需要选c0个集合内的数,选c1个集合外的数for(int i=1;i<=n;i++) {if(vis[i]) {//在集合内if(c0>0) {c0--;num.push_back(i);}} else {//不在集合内if(c1>0) {c1--;num.push_back(i);}}}for(auto it:num) {//更新一下每个数是否属于集合的状态vis[it]^=1;}ans^=query(num);}printf("! %d\n",ans);return 0;
}

CodeForces - 1534E Lost Array(bfs+交互)相关推荐

  1. Codeforces 1054D Changing Array

    Codeforces 1054D Changing Array 做法:给定一个序列,每个数可以把在2进制k位下取反,也可以不变,在改变后,这个序列异或和不为0的区间的个数.考虑如何求出尽可能少的异或为 ...

  2. CodeForces - 1610B Kalindrome Array

    B. Kalindrome Array time limit per test1 second memory limit per test256 megabytes An array [b1,b2,- ...

  3. codeforces EDU suffix array

    本文以codeforces EDU suffix array为资料 content introdcution suffix array height array lcp problems & ...

  4. [codeforces 1343B] Balanced Array 奇+奇=偶,奇+偶=奇,偶+偶=偶

    Codeforces Round #636 (Div. 3)   比赛人数12253 [codeforces 1343B]   Balanced Array   奇+奇=偶,奇+偶=奇,偶+偶=偶 总 ...

  5. CodeForces - 1353D Constructing the Array(bfs)

    题目链接:点击查看 题目大意:给出一个长度为 n ,初始时全部为 0 的数组 a ,后续进行 n 次操作,每次操作找到最长的连续 0 ,如果有多个则选择位置最靠左边的,将这组连续 0 的,最中间位置的 ...

  6. CodeForces - 1539F Strange Array(线段树区间合并)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,规定位置 iii 的贡献是:设 x=a[i]x=a[i]x=a[i],选择一个包含 iii 的区间 [l,r][l,r][l,r],将其中 ...

  7. codeforces 1367B - Even Array

    B. Even Array time limit per test2 seconds memory limit per test256 megabytes inputstandard input ou ...

  8. 【Codeforces】808D Array Division(前后两部分和相等)

    http://codeforces.com/contest/808/problem/D 给你一个数组,问:是否可以通过移动一个数字的位置,求只能移动一次,使得这个数组前后部分的和相等,前后部分不一定等 ...

  9. Codeforces 494D Upgrading Array

    http://codeforces.com/contest/494/problem/D 题意:给一个数组,和一个坏质数集合,可以无数次地让1到i这些所有数字除以他们的gcd,然后要求Σf(a[i])的 ...

最新文章

  1. Redis 秒杀实战
  2. linux ifconfig命令配置ip地址
  3. 虚拟化何以四两拨千斤
  4. ngx_pagespeed加速nginx
  5. 成功解决internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'express'
  6. 再见!妈妈再也不用担心我的计算机基础!
  7. Hibernate 一级缓存,二级缓存,查询缓存
  8. 通过 url 参数 parameters 和 script tag 属性来配置 SAP UI5 运行时
  9. fe文件服务器,FE File Explorer
  10. iOS开发造轮子 | 通用占位图
  11. Web黑客工具箱之LiveHttpHeaders
  12. JS获取页面中Url的某个参数
  13. android打印json对象,android之json数据过长打印不全问题的解决
  14. win10 mysql登录密码忘了_64位 windows10,MYSQL8.0.13重置密码(忘记密码或者无法登录)...
  15. toad可以连接mysql吗_配置Toad链接远程Oracle数据库
  16. php5.1 0day,关于phpwind 5.01-5.3 0day的分析
  17. 金蝶财务软件有哪些缺点
  18. java单继承多实现_单继承,多实现
  19. HTML初学(简单html文件、简单表格布局)
  20. Dynamic Scoping (动态范围)

热门文章

  1. 前端三大技术 HTML、CSS、JavaScript 快速入门手册
  2. 「我去,这也能行!」令人惊叹的8个深度学习应用
  3. linux c 多线程socket编程,Linux多线程socket编程一些心得
  4. pexpect oracle,expect免交互脚本编程
  5. java实现rsa欧几里得算法求d_RSA 加密算法的 java 实现
  6. 认证(登录)功能实现
  7. 课程分类管理-添加课程分类
  8. 在vs code中创建代码片段
  9. 文件操作-小文件复制
  10. 使用PyCharm定义QQ变量