当前位置:网站首页>Codeforces Round #713 (Div. 3) E(思维)
Codeforces Round #713 (Div. 3) E(思维)
2022-08-08 19:00:00 【Here_SDUT】
E. Permutation by Sum
题意:
给你一个排列(1到n每个数字都出现且只出现一次),要求你用排列中的数字构造一个新数组,使得下标为l到r的数字之和等于s。
分析:
题目可以理解为,让你在1-n中挑k(r-l+1)个数字和为s。首先可以知道可以组成的数的理论最大值为 \displaystyle \sum_{i = n-k}^n i,理论最小值为\displaystyle \sum_{i = 1}^k i,并且处于这个范围之内的数都可以被正确的表示出来。故,只要s处于最小值和最大值之间就有解。
剩下就是如何找这k个数,定义两个函数:low(k)表示\displaystyle \sum_{i = 1}^k i,high(k,n) 表示\displaystyle \sum_{i = n-k}^n i,即分别表示从1-n中取k个的最小值和最大值。我们令i = n,如果high(k, i) >= s && low(k - 1) <= s - i则表示i这个数字可以选取。其中式子的前半段表示,从1-i这些数中取k个数可以表达出s,后半段表示,如果选取i,那么在剩下的数中取k-1个数也可以表示出s-i,那么i这个数可以选用。
代码
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
typedef pair<int, int> PII;
int sum[1111];
bool vis[1111];
int n, l, r, s, m;
int f;
int low(int k) { return sum[k]; }
int high(int k, int i) { return sum[i] - sum[i - k]; }
int ans[1111];
int main(int argc, char const *argv[]) {
int t;
cin >> t;
for (int i = 1; i <= 510; i++) sum[i] = sum[i - 1] + i;
while (t--) {
cin >> n >> l >> r >> s;
memset(vis, 0, sizeof vis);
int k = r - l + 1, i = n;
int now = l;
while (k && i >= 1) {
if (high(k, i) >= s && low(k - 1) <= s - i) {
ans[now++] = i;
vis[i] = 1;
k--;
s -= i;
}
i--;
}
if (s > 0)
puts("-1");
else {
int now = 1;
for (int i = 1; i <= n; i++) {
if (l <= i && i <= r) continue;
while (vis[now] == 1) now++;
ans[i] = now++;
}
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
puts("");
}
}
return 0;
}边栏推荐
- PG's huge page
- nyoj 712 探 寻 宝 藏(双线dp 第六届河南省程序设计大赛)
- Michael Bronstein 系列长文:迈向几何深度学习(之三)——第一个几何神经网络模型
- PX4-Things you need to know for secondary development of flight control-Cxm
- Dandelion R300A 4G router, remote monitoring PLC tutorial
- What are the three main aspects of digital factory construction?
- 请问shake数据库中mongoshake同步过程中,src_mongo挂了,同步服务不会退出吗?
- hdu1042 N!(大数)
- PG 之 huge page
- oracle视图v$active_session_history,dba_hist_active_session_history如何记录IP地址
猜你喜欢

Oracle - table

搭建DG导致归档日志量变多问题排查

Advanced CAD practice (2)

Fortinet new cloud native protection products launched amazon cloud platform of science and technology

C language elementary - structure

Smobiler的复杂控件的由来与创造

软考中级网络工程师全面学习笔记第2版(5万字)+配套视频及课件

什么是Shell?从小白到入门你只差一个它

Ability in general, but it can be large horizontal jump freely?Where is the better?

Redis之SDS数据结构
随机推荐
请问shake数据库中mongoshake同步过程中,src_mongo挂了,同步服务不会退出吗?
5 IPOs, Internet home improvement is not as simple as Tubatu thinks
MogDB学习笔记-从0开始
ORACLE子查询 导致无法谓词推入的研究
Vue program of web cache problem after packaging
Codeforces Round #705 (Div. 2)
Codeforces Round #721 (Div. 2)
01、前言
Open Office XML 格式中的 Style 设计原理
什么是Shell?从小白到入门你只差一个它
The history of cartoon rendering
PG's huge page
Fortinet全新云原生保护产品上线亚马逊云科技平台
Redis之SDS数据结构
企业进行知识共享的好处有哪些?
nyoj714 Card Trick(第六届河南省程序设计大赛)
Advanced CAD practice (2)
软件测试基础笔记
hdu1042 N! (large number)
Geometric g6 will carry harmonyos system, a comprehensive upgrade competitiveness of products