zoukankan      html  css  js  c++  java
  • A1113 Integer Set Partition

    在这里插入图片描述

    #include<iostream>
    #include<vector>
    #include<map>
    #include<string>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn = 100001;
    int a[maxn];
    int main()
    {
    	int n, s1 = 0, s2 = 0;
    	cin >> n;
    	for (int i = 0; i < n; i++)
    	{
    		cin >> a[i];
    	}
    	sort(a, a + n);
    	for (int i = 0; i < n; i++)
    	{
    		if (i < n / 2) s1 += a[i];
    		else s2 += a[i];
    	}
    	cout << n % 2 << " " << s2 - s1 << endl;
    }
    

    快排的Partition
    据说复杂度可以降到O(n)结果有个点超时了。

    #include<iostream>
    #include<vector>
    #include<map>
    #include<string>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn = 100001;
    int a[maxn];
    int Kth_elem(int a[], int low, int high, int k) {
    	swap(a[low], a[(low + high) / 2]);
    	int pivot = a[low];
    	int low_temp = low, high_temp = high;
    	while (low < high) {
    		while (low < high && a[high] >= pivot) high--;
    		a[low] = a[high];
    		while (low < high && a[low] <= pivot) low++;
    		a[high] = a[low];
    	}
    	a[low] = pivot;
    	if (low == k) return a[low];
    	else if (low > k)
    		return Kth_elem(a, low_temp, low - 1, k);
    	else return Kth_elem(a, low + 1, high_temp, k);
    }
    
    int main()
    {
    	int n, s1 = 0, s2 = 0;
    	cin >> n;
    	for (int i = 0; i < n; i++)
    	{
    		cin >> a[i];
    	}
    	Kth_elem(a, 0, n - 1, n / 2);
    	for (int i = 0; i < n; i++)
    	{
    		if (i < n / 2) s1 += a[i];
    		else s2 += a[i];
    	}
    	cout << n % 2 << " " << s2 - s1 << endl;
    }
    
  • 相关阅读:
    Rabbitmq 性能测试
    B+树图文详解
    图的概念和存储(邻接矩阵,邻接表)
    WebApi系列文章
    Asp.Net MVC项目集成Swagger
    正则表达式匹配两个特殊字符中间的内容
    数学常数e的含义
    十大排序算法总结
    C#集合类型大揭秘
    深入System.Web.Caching命名空间 教你Hold住缓存管理(三)
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811976.html
Copyright © 2011-2022 走看看