题目大意:给定一个输入规律是
a
1
,
a
3
,
…
,
a
4
,
a
2
a_1,a_3,\dots,a_4,a_2
a1,a3,…,a4,a2的数列,输出原数列
模拟
把给定的数列前
n
+
1
2
\frac{n+1}{2}
2n+1个位置放到答案的奇数位置上,把后面的位置放到答案的偶数位置上即可
#include<bits/stdc++.h>usingnamespace std;#define IOS ios::sync_with_stdio(0)#define LL long long#define maxn (int)(2e5 + 10)int a[maxn];int ans[maxn];int main (){
IOS;int T ; cin >> T;for(int cas =1; cas <= T ; cas++){int n ; cin >> n;for(int i =1; i <= n ; i++) cin >> a[i];for(int i =1; i <=(n+1)/2; i++)
ans[2*i-1]= a[i];for(int i = n ; i >(n+1)/2; i--)
ans[(n-i+1)*2]= a[i];for(int i =1; i <= n ; i++)
cout << ans[i]<<" ";
cout << endl;}}
要使得我们构造的数字是最小的,我们就应该尽量让末位的数字尽可能大,因此,我们让x从d=9开始减,减到最后
x
≤
d
x\le d
x≤d的时候,我们所得到的数字拼起来,就是我们要的答案
#include<bits/stdc++.h>usingnamespace std;#define IOS ios::sync_with_stdio(0)#define LL long long#define maxn (int)(2e5 + 10)
LL ans[maxn];int main (){
IOS;int T ; cin >> T;for(int cas =1; cas <= T ; cas++){int x ; cin >> x;if( x >45)
ans[cas]=-1;elseif( x <=9)
ans[cas]= x;else{
vector <int> res;int d =9;while( x > d ){
res.push_back(d);
x -= d;
d --;}
res.push_back(x);sort(res.begin(),res.end());
LL cnt =0;for(auto y : res )
cnt = cnt *10+ y;
ans[cas]= cnt;}}for(int i =1; i <= T ; i++)
cout << ans[i]<< endl;}
#include<bits/stdc++.h>usingnamespace std;#define IOS ios::sync_with_stdio(0)#define LL long long#define maxn (int)(2e5 + 10)int ans[maxn];int a[maxn];int pre[maxn];int main (){
IOS;int T ; cin >> T;for(int cas =1; cas <= T ; cas++){int n ; cin >> n;int sum =0;
memset ( pre ,0,(n+5)*sizeof(int));for(int i =1; i <= n ; i++) cin >> a[i], sum += a[i];
pre[1]= a[1];for(int i =2; i <= n ; i++) pre[i]= pre[i-1]+ a[i];int res =1<<30;for(int i =1; i*i <= sum ; i++){if( sum % i )continue;int fac = sum / i;int cur =0;bool flag =1;for(int j =1; j <= n ; j++){if( pre[j]- pre[cur]> i ){
flag =0;break;}elseif( pre[j]- pre[cur]== i )
cur = j ;if( j == n && j != cur )
flag =0;}if( flag )
res = min ( res , n -fac );
cur =0;
flag =1;for(int j =1; j <= n ; j++){if( pre[j]- pre[cur]> fac ){
flag =0;break;}elseif( pre[j]- pre[cur]== fac )
cur = j ;if( j == n && j != cur )
flag =0;}if( flag )
res = min ( res , n-i );}
ans[cas]= res;}for(int i =1; i <= T ; i++)
cout << ans[i]<< endl;}
我们对数组进行排序,假设我们得到的一个最小的数字是
a
1
a_1
a1,最后一个比它大不超过2的位置是
a
s
a_s
as,也就是说,我们可以从2到s当中挑选两个不一样的数字,那么对答案的贡献就是
C
s
−
1
2
C_{s-1}^2
Cs−12
#include<bits/stdc++.h>usingnamespace std;#define IOS ios::sync_with_stdio(0)#define LL long long#define maxn (int)(2e5 + 10)
LL ans[maxn];int a[maxn];int main (){
IOS;int T ; cin >> T;for(int cas =1; cas <= T ; cas++){int n ; cin >> n;for(int i =1; i <= n ; i++) cin >> a[i];
sort ( a+1, a+1+n );
LL res =0;for(int i =1; i <= n-2; i++){int pos ;if( a[i]+2> a[n]) pos = n;else pos =upper_bound(a+1,a+1+n,a[i]+2)- a -1;
LL tmp = pos - i ;
res += tmp*(tmp-1)/2;}
ans[cas]= res;}for(int i =1; i <= T ; i++)
cout << ans[i]<< endl;}
进一步地,如果要跳远m个数字,我们只要找到了满足条件的
a
s
a_s
as,我们对答案的贡献就是
C
s
−
1
m
−
1
C_{s-1}^{m-1}
Cs−1m−1
#include<bits/stdc++.h>usingnamespace std;#define IOS ios::sync_with_stdio(0)#define LL long long#define maxn (int)(2e5 + 10)const LL mod =1e9+7;int m , k ;
LL fac[maxn];
LL inv[maxn];
LL quick_pow ( LL n , LL k ){
LL res =1;while( k ){if( k &1) res = res * n % mod;
n = n * n % mod;
k >>=1;}return res;}
LL get_inv ( LL n ){return quick_pow ( n , mod-2);}voidget_fac(){
fac[0]=1;for(int i =1; i <=2e5; i++)
fac[i]= fac[i-1]* i % mod;
inv[200000]=get_inv(fac[200000]);for(int i =200000-1; i >=0; i--)
inv[i]= inv[i+1]*(i+1)% mod;}
LL C (int n ){if( n < m-1)return0;return fac[n]* inv[m-1]% mod * inv[n-m+1]% mod;}
LL ans[maxn];int a[maxn];int main (){
IOS;get_fac();int T ; cin >> T;for(int cas =1; cas <= T ; cas++){int n ; cin >> n;
cin >> m >> k;for(int i =1; i <= n ; i++) cin >> a[i];
sort ( a+1, a+1+n );
LL res =0;for(int i =1; i <= n-m+1; i++){int pos ;if( a[i]+ k > a[n]) pos = n;else pos =upper_bound(a+1,a+1+n,a[i]+k)- a -1;
LL tmp = pos - i ;
res =( res +C(tmp))% mod;}
ans[cas]= res;}for(int i =1; i <= T ; i++)
cout << ans[i]<< endl;}