题目大意:给定一个长度为n的数组a,我们可以从下标1~n-1中选取一个数字flag从而分成两段,并且对这[1,flag] [flag,n]两区间的数值进行非降序排序(不严格升序)。
如果整个序列没有按不严格升序排序就输出YES,否则为NO。
input
3 3 2 2 1 4 3 1 2 1 5 1 2 2 4 4output
YES YES NO
发现其实只要一个数字存在不在它该在的位置上,就变换不了成非降序序列。(因为它的可操作性巨强,下标存在于1~n-1的范围中)
#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<cstring>
#include<cstdlib>
#include<map>
#include<algorithm>
using namespace std;
int a[200200],b[200200];
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=a[i];
}
sort(a+1,a+1+n);
bool flag=false;
for(int i=1;i<=n;i++)
{
if(a[i]!=b[i]) flag=true;//非降序
}
if(flag==false) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}
题目大意:给定一个大小为n的数组a,求出它所有子段的值之和。
它的值的定义是分段数+所有分段的MEX之和。(mex:没有出现过的最小的自然数)
如果可以通过从开头删除几个(可能是零个或全部)元素和从末尾删除几个(可能是零个或全部)元素来从y获得x,则数组x是数组y的子段。
input
4 2 1 2 3 2 0 1 4 2 0 5 1 5 0 1 1 0 1output
4 14 26 48
在代码中参与计算的第一个for循环,很显然易见就是求的是区间长度总和嘛!比如像[1 3 5 4 2],分多个子段那不就是单个长度的有5个,双个长度的有4个,三个的有3个,四个的有2个,五个的有1个···以此类推其他区间的长度。
知道了区间长度那么就该求的是每个区间的mex了。就很神奇,看图get
其实为什么找0就行了呢?因为如果不是0的数字的时候,它所参与的区间都没准能够找到更小的数字或者需要更大的数字来填充这都是不一定的,所以没有太大意义,但从单个上来说他们都是0,加不加无所谓。所以我们只需要看到0的位置以及它参与的区间数量即可。
区间数量就等于个数!!!
【为什么昨晚写的时候也是这个思路这些数字但是就没有看出0的求法来呢???(自闭)】
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<cstring>
#include<cstdlib>
#include<map>
#include<algorithm>
using namespace std;
int a[200200];
int main()
{
//cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
int sum=0;
for(int i=1;i<=n;i++)
sum+=i*(n-i+1);
//cout<<sum<<endl;
for(int i=1;i<=n;i++)
{
if(a[i]==0) sum+=i*(n-i+1);
}
cout<<sum<<endl;
}
return 0;
}
题目大意:给定一个长度为n的序列a,让我们把从下标2~n-1的数全部挪到下标1或者下标n的地方。挪动的规则是:Select(i,j,k):选择3个索引1≤i<j<k≤n,使得第j个桩包含至少2个石头,然后他从桩j中取出2个石头,并将一个石头放入i堆中,令一块石头放入k堆中。
问我们:将所有的石头移到堆1和堆n所需的最少操作次数是多少?或者确定是否不可能。
不可能就输出-1。
input
4 5 1 2 2 3 6 3 1 3 1 3 1 2 1 4 3 1 1 2output
4 -1 1 -1
撇开a1和an,看2~n-1之中才能知道操作步数,如果是偶数的话直接➗2,奇数的话➗2再向上取整。这里就能得到最小步数。
那么-1的情况怎么看呢?偶数情况下是一定可以分完的所以不用思考,奇数的话题目给定j中必须要>=2才能参与挪动,所以可以肯定的是当中间一排全部都是1的时候他就挪不动了;从所给样例中可以看出当存在n==3并且中间为奇数时其实也就相当于中间夹了一个1他也就挪不动了。
其余的数字都可以通过或多或少的挪动次数来移到两侧。
#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<cstring>
#include<cstdlib>
#include<map>
#include<algorithm>
using namespace std;
int a[200200];
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
long long int t;
cin>>t;
while(t--)
{
long long int n,sum=0;
cin>>n;
for(long long int i=1;i<=n;i++)
{
cin>>a[i];
if(i>=2&&i<=n-1)
{
if(a[i]%2==1) sum+=(a[i]+1)/2;
else sum+=a[i]/2;
}
}
long long int sum1=0;
for(long long int i=2;i<=n-1;i++)
{
if(a[i]==1) sum1++;
}
if((n==3&&a[2]%2==1)||sum1==n-2) cout<<"-1"<<endl;
else cout<<sum<<endl;
}
return 0;
}
这里有一个很坑的点就是它需要开LL,不然会wa欸
————————————
D啊!dp啊,改天再来。。。