很意思的一道构造题

题意:给一个\(n、k\),让构造长度为n的数组满足,子数组为整数的个数为k个,负数的为\(k-(n+1)* n/2\),每个数的范围为\([-1000,1000]\)

这种构造题可以考虑就是前一段可以一直用一样的、最小的。

我们观察可以发现\(k+k-(n+1)* n/2= (n+1)* n/2\)

也就是所有子数组的个数,换句话说子数组不能有0。

这样我们很容易考虑用很小的一个负数和一个很小的正数去构造

这里我用的是\(1\)和\(-1000\)

我们先考考虑一下前一段是p个1,后面全是-1000的情况这样我们得到的正数组有\(\frac{(p+1) * p}{2}个\)

当\(k=\frac{(p+1) * p}{2}\)时,自然皆大欢喜

当\(k>=\frac{(p+1) * p}{2}\)时,我们考虑一下剩下的\(k-\frac{(p+1) * p}{2}\)该如何臭凑出来,能增加p吗?,当p+1,我们会增加p+1个正数组,这是不行的,我们考虑的p的最大满足\(k>=\frac{(p+1) * p}{2}\)的p,也就是说缺少的正数组个数是在\([1,p]\)

我们可以选择前面p个1中的一个将其变为1000,\(p+1\)处的-1000遍为500,这样我们就可以添加\([1,p]\)个正数组,哪个位置的1变为1000呢?

我们可以找一下规律



弄清楚上面的事情,代码就很简单了,我们只需要而分出最后一个满足条件的p然后按照上面的构造方法放数即可

#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back using namespace std; const int N=5e3+10,mod=100003,inf=(1ull<<63)-1;
int n,m,k;
int vis[N],d[N];
int a[1010],b[1010]; void solve()
{
cin>>n>>k;
int l=0,r=n;
while(l<r){
int mid=(l+r+1)>>1;
if(mid*(mid+1)/2<=k) l=mid;
else r=mid-1;
}
if(l*(l+1)/2==k){
rep(i,1,l) cout<<1<<' ';
rep(i,l+1,n){
if(i==l+1) cout<<-500<<' ';
else cout<<-1000<<' ';
}
cout<<endl;
}else{
int d=k-(l*(l+1))/2;
rep(i,1,l){
if(i==d) cout<<1000<<' ';
else cout<<1<<' ';
}
rep(i,l+1,n){
if(i==l+1) cout<<-500<<' ';
else cout<<-1000<<' ';
}
cout<<endl;
}
} signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
cin>>_;
while(_--)
solve();
return 0;
}

Educational Codeforces Round 145 (Rated for Div. 2)C. Sum on Subarrays(构造)的更多相关文章

  1. Educational Codeforces Round 62 (Rated for Div. 2)E(染色DP,构造,思维,组合数学)

    #include<bits/stdc++.h>using namespace std;const long long mod=998244353;long long f[200007][2 ...

  2. Educational Codeforces Round 95 (Rated for Div. 2) B. Negative Prefixes (贪心,构造)

    题意:给你一串长度为\(n\)的序列,有的位置被锁上了,你可以对没锁的位置上的元素任意排序,使得最后一个\(\le0\)的前缀和的位置最小,求重新排序后的序列. 题解:贪心,将所有能动的位置从大到小排 ...

  3. Educational Codeforces Round 94 (Rated for Div. 2) C. Binary String Reconstruction (构造)

    题意:给你一个字符串\(s\),原字符串为\(w\),如果\(i>x\)且\(w_{i-x}=1\),那么\(s_{i}=1\),如果\(i+x\le n\)且\(w_{i+x}=1\),那么\ ...

  4. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  5. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  6. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  7. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  8. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  10. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

随机推荐

  1. kettle系统列文章03---如何建立一个作业

    上篇文章我们建立好了转换,我们希望这个转换可以做成定时任务,每一分钟执行一次 第一步:创建作业开始节点:文件---->新建---->作业----核心对象---->通用-----> ...

  2. yum 安装失败解决思路$releasever(curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error")

    问题 公司使用刀片机的系统版本是CentOS 7.9.2009(Core),本人在重新安装虚拟机时,也使用对应的系统版本,在安装软件时,yum无法正常使用,一开始觉得,centos的release版本 ...

  3. 【3】VSCode 主题设置推荐,自定义配色方案,修改注释高亮颜色

    相关文章: [一]tensorflow安装.常用python镜像源.tensorflow 深度学习强化学习教学 [二]tensorflow调试报错.tensorflow 深度学习强化学习教学 [三]t ...

  4. window hadoop yarn任务执行失败:ExitCodeException exitCode=-1073741515

    环境 window server 2019 单机版hadoop3.3.1 和 winutils.exe 异常 winutils.exe task create -m -1 -c -1 continer ...

  5. 遥感图像处理笔记之【Machine Learning CS-433 - Class Project 2 - Road Segmentation - EPFL】

    遥感图像处理学习(8) 前言 遥感系列第8篇.遥感图像处理方向的学习者可以参考或者复刻 本文初编辑于2024年1月2日 本文再编辑于2024年1月4日:附作者改进U-Net网络图片:文字补充说明 20 ...

  6. element-ui表格筛选,根据表头属性显示隐藏列

    效果: 步骤: 1.标签上添加要过滤的源数组 <el-table-column label="标签" :filters="filterList" filt ...

  7. Leetcode刷题第六天-回溯

    131:分割回文串 链接:131. 分割回文串 - 力扣(LeetCode) for 遍历字符串 递归切割,切割到字符串尾,单次结束 1 class Solution: 2 def partition ...

  8. java获取最近12个月月份

    最近在做一个换电站管理的项目,其中有一个大屏折线图.要求计算近12个月的数据.所以,就需要写一个生成近12个月月份的算法.算法如下. 一:编写生成近12个月月份的算法 二:编写判断当天是否是月初的算法 ...

  9. KVM环境:Active console session exists for this domain

    做测试过程中被迫换电脑,但没有关掉原电脑的连接,所以用其他电脑连接测试环境时,发现之前的kvm测试环境因没有断开,无法连接: error: operation failed: Active conso ...

  10. 21.1 动态TLS--《Windows核心编程》

    应用程序通过调用一组4个函数来使用动态 TLS,这些函数实际上最经常为 DLL 所使用. 通常情况下,如果DLL使用 TLS,那么当它用 DLL_PROCESS_ATTACH 标志调用它的 DllMa ...