当前位置: 首页 > news >正文

Codeforces Round 1054 B. Unconventional Pairs(2149)

https://codeforces.com/contest/2149/problem/B

题意:

解题思路:

通过最后提示:可以发现,是偶数个元素,并且成对出现是排序过,然后求2个数之差,然后取最大的差值。

根据题意,是求,成对出现的数据之差,其差值,最大的是?

最小可能的,最大差值,在元素的对对中,根据NOTE就可以推理出来。

参考和别人的代码,消化理解完成。


/*
For a pair(x,y),its difference is defined as |x-y|.
Pet wants to form unconventional pairs such that the maximum difference amongall pairs is minimized.Detemine the minimun possible value of this maximum difference. Example
Input
5
2
1 2
4
10 1 2 9
6
3 8 9 3 3 2
8
5 5 5 5 5 5 5 5
4
-5 -1 2 6
Output
1
1
1
0
4Note
In the first test case, the array is: [1,2]. 
The only possible (and therefore optimal) pair is (1,2), 
its difference is |1-2|=1,the answer is 1.In the second test case, the array is: [10,1,2,9]. 
We can choose pairs — (1,2) and (9,10): both differences are equal to 1, 
therefore, the maximum difference is 1.In the third test case, the array is: [3,8,9,3,3,2]. 
We can choose pairs: (2,3), (3,3), (8,9). The differences are: 1,0,1
-the largest is 1.
*/#include <iostream>
using namespace std;//no used,for time to exceed!
void sort_bubble_by_asc(int *list,int len)
{	for (int i = 0; i < len - 1; i++){for (int j = 0; j < len - 1 - i; j++){if (list[j] > list[j + 1]){swap(list[j], list[j + 1]);}}}
}//Quick Query
int GetPositionBYQueckSort(int *list, int front, int back){int midPos = 0;int left = front;int right = back;while (left < right){//from right to startwhile (left < right && list[left] < list[right]){right--;}//to swapif (left < right){swap(list[left], list[right]);left++;}//right partwhile (left < right && list[left] < list[right]){left++;}if (left < right){swap(list[left], list[right]);right--;}}midPos = left;return midPos;
}
void sort_by_quick_exercise01(int *list, int front, int back)
{if (front < back){int mid = GetPositionBYQueckSort(list, front, back);sort_by_quick_exercise01(list, front, mid - 1); //front partsort_by_quick_exercise01(list, mid + 1, back); //back part}
}int get_max_value(int a, int b)
{if (a > b){return a;}else{return b;}
}int main()
{int t=0;int v1=0;int n=0;cin >> t;while (t--){		cin >> n;int *list = new int[n];for (int i = 0; i < n;i++){cin >> list[i];}//asc or des , all ok!//sort_bubble_by_asc(list, n); //this is time to exceed!!!sort_by_quick_exercise01(list,0,n-1);int rs = 0;int different = 0;// pair ,so i=i+2for (int i = 0; i < n; i += 2){different = list[i + 1] - list[i];rs = get_max_value(rs, different);}cout << rs <<endl;delete[] list;}return 0;
}

说明:不能用冒泡排序,当测试数据2万条的时候,就会超时,根据之前排序方法,只有快速排序

循环次数最少。

完成。

http://www.dtcms.com/a/443022.html

相关文章:

  • 如何做公众号微信杭州百度seo优化
  • 个人网站备案号可以做企业网站吗成都门户网站有哪些
  • 深圳网站建设力荐上榜网络做网站需要具备什么
  • 帝国cms网站迁移做网站杭州
  • 在线建站模板熊掌号wordpress
  • 网站可以自己建立吗黄石网站建设
  • 做网站购买服务器网站集约化平台
  • 做图片视频的网站免费域名注册 ddns
  • leetcode 36 有效的数独
  • 使用hhblits进行序列比对
  • 网站域名过期杭州网站建设哪家公司好
  • 桂林北站是哪个区wordpress 免费企业网站 模板下载
  • 逻辑门可以实现数学运算,逻辑思维与数学思维谁更基本
  • [光学原理与应用-484]:《中安DF-300S技术规格书》
  • 做的比较好的卡车网站威县做网站哪儿好
  • 安阳网站建设哪家正规温州建设小学网站首页
  • 如何做律师网站响应式网站模板html5
  • 永嘉网站建设域名年费价格表
  • 网站开发的功能需求怎么写学院网站建设管理规章制度
  • 响应式网站好处公司网站建设情况说明
  • Java 21 发行说明总结
  • 北京社保网上服务平台官网trinseo公司
  • 俄文网站制作要制作网站
  • 龙岗网站建设定制开发整合营销传播的效果表现为
  • 资兴市住房和城乡建设局网站网页制作素材动物
  • 网站经营方案 备案网站建设都需要哪些材料
  • 开网站是干什么的沈阳企业免费建站
  • 盐城网站设计公司网站如何推广运营
  • 做消防哪些网站找工作免费咨询医生回答在线
  • 运城网站建设费用苏州建设工程招标官方网站