B:http://acm.hdu.edu.cn/showproblem.php?pid=5247
Problem Description
小度熊拿到了一个无序的数组,对于这个数组,小度熊想知道是否能找到一个k 的区间,里面的 k 个数字排完序后是连续的。
现在小度熊增加题目难度,他不想知道是否有这样的 k 的区间,而是想知道有几个这样的 k 的区间。
现在小度熊增加题目难度,他不想知道是否有这样的 k 的区间,而是想知道有几个这样的 k 的区间。
Input
输入包含一组测试数据。
第一行包含两个整数n,m,n代表数组中有多少个数字,m 代表针对于此数组的询问次数,n不会超过10的4次方,m 不会超过1000。第二行包含n个正整数,第 I 个数字代表无序数组的第 I 位上的数字,数字大小不会超过2的31次方。接下来 m 行,每行一个正整数 k,含义详见题目描述,k 的大小不会超过1000。
第一行包含两个整数n,m,n代表数组中有多少个数字,m 代表针对于此数组的询问次数,n不会超过10的4次方,m 不会超过1000。第二行包含n个正整数,第 I 个数字代表无序数组的第 I 位上的数字,数字大小不会超过2的31次方。接下来 m 行,每行一个正整数 k,含义详见题目描述,k 的大小不会超过1000。
Output
第一行输"Case #i:"。(由于只有一组样例,只输出”Case #1:”即可)
然后对于每个询问的 k,输出一行包含一个整数,代表数组中满足条件的 k 的大小的区间的数量。
然后对于每个询问的 k,输出一行包含一个整数,代表数组中满足条件的 k 的大小的区间的数量。
Sample Input
6 2 3 2 1 4 3 5 3 4
Sample Output
Case #1: 2 2#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> using namespace std; const int maxn=10010; int N,M; int a[maxn]; int ans[maxn],pre[maxn]; int minv[maxn],maxv[maxn]; bool vis[maxn]; map<int,int> mp; void solve() { mp.clear(); for(int i=1;i<=N;i++) { maxv[i]=minv[i]=a[i]; vis[i]=1; if(mp.find(a[i])==mp.end())pre[i]=-1; else pre[i]=mp[a[i]]; mp[a[i]]=i; } for(int i=1;i<=1000;i++) { ans[i]=0; if(i>N)break; for(int j=1;j+i<=N+1;j++) { if(!vis[j])continue; if(maxv[j]-minv[j]+1!=i)continue; ans[i]++;// 俩个条件 一个是最大减去最小为区间长度-1 另外一个就是所有的数都只出现一次 } for(int j=1;j+i<=N;j++) { maxv[j]=max(maxv[j],a[i+j]); minv[j]=min(minv[j],a[i+j]); if(pre[i+j]>=j) vis[j]=false; } } } int main() { int cas=1; while(scanf("%d%d",&N,&M)!=EOF) { for(int i=1;i<=N;i++)scanf("%d",&a[i]); solve(); //预处理所有的 printf("Case #%d:\n",cas++); while(M--) { int x; scanf("%d",&x); printf("%d\n",ans[x]); } } return 0; }
C: http://acm.hdu.edu.cn/showproblem.php?pid=5248
Problem Description
给定序列
A={A1,A2,...,An}
, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:
Bi<Bi+1,1≤i<N
)。
我们定义从序列A到序列B变换的代价为 cost(A,B)=max(|Ai−Bi|)(1≤i≤N) 。
请求出满足条件的最小代价。
注意,每个元素在变换前后都是整数。
我们定义从序列A到序列B变换的代价为 cost(A,B)=max(|Ai−Bi|)(1≤i≤N) 。
请求出满足条件的最小代价。
注意,每个元素在变换前后都是整数。
Input
第一行为测试的组数
T(1≤T≤10)
.
对于每一组:
第一行为序列A的长度 N(1≤N≤105) ,第二行包含N个数, A1,A2,...,An .
序列A中的每个元素的值是正整数且不超过 106 。
对于每一组:
第一行为序列A的长度 N(1≤N≤105) ,第二行包含N个数, A1,A2,...,An .
序列A中的每个元素的值是正整数且不超过 106 。
Output
对于每一个测试样例,输出两行:
第一行输出:"Case #i:"。i代表第 i 组测试数据。
第二行输出一个正整数,代表满足条件的最小代价。
第一行输出:"Case #i:"。i代表第 i 组测试数据。
第二行输出一个正整数,代表满足条件的最小代价。
Sample Input
2 2 1 10 3 2 5 4
Sample Output
Case #1: 0 Case #2: 1简单二分#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> using namespace std; const int maxn=100010; int N; int a[maxn]; bool can(int x) { int last=a[1]-x; for(int i=2;i<=N;i++) { if(a[i]+x<=last)return false; last=max(a[i]-x,last+1); } return true; } int main() { int T,cas=1; scanf("%d",&T); while(T--) { scanf("%d",&N); int sum=0; for(int i=1;i<=N;i++)scanf("%d",&a[i]); int l=0,r=1e6; while(l<r) { int mid=(l+r)>>1; if(can(mid))r=mid; else l=mid+1; } printf("Case #%d:\n%d\n",cas++,l); } return 0; }