zoukankan      html  css  js  c++  java
  • 【搜索】POJ-2718 贪心+枚举

    一、题目

    Description

    Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0.

    Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0. 
    For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. Of course, there are many ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.

    Input

    The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0, 1, ..., 9.) No digit appears more than once in one line of the input. The digits will appear in increasing order, separated by exactly one blank space.

    Output

    For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.

    Sample Input

    1
    0 1 2 4 6 7
    
    

    Sample Output

    28
    

    二、思路&心得

    • 贪心:根据题目特点,选择不同情况下的最优解
    • 枚举:枚举多种局部最优解,然后求出符合题意的最值

    三、代码

    
    #include<cstdio>
    #include<algorithm>
    #define MAX 99999
    using namespace std;
    
    int nums[11];
    char ch;
    
    int solve() {
    	int len = 0;
    	while (1) {
    		scanf("%d%c", &nums[len ++], &ch);
    		if (ch == '
    ') break;
    	}
    	if (len == 2) {
    		return abs(nums[0] - nums[1]);
    	}
    	int x = 0, y = 0;
    	int mid;
    	if (len % 2 != 0) {
    		mid = len / 2;
    		if (!nums[0]) swap(nums[0], nums[1]);
    		for (int i = 0; i < mid + 1; i ++) {
    			x = x * 10 + nums[i];
    		}
    		for (int i = len - 1; i > mid; i --) {
    			y = y * 10 + nums[i];
    		}
    		return x - y;
    	} else {
    		int index, cnt = 0, ans = MAX; 
    		int min_XY = 11;
    		for (int i = 1; i < len; i ++) {
    			if (min_XY >= nums[i] - nums[i - 1] && nums[i] && nums[i - 1]) {
    				min_XY = nums[i] - nums[i - 1];
    				x = nums[i], y = nums[i - 1];
    				for (cnt = 0, index = 0; index < len, cnt < (len - 2) / 2; index ++) {
    					if (index != i && index != i - 1) {
    						x = x * 10 + nums[index];
    						cnt ++;
    					}
    				}
    				mid = index;
    				for (cnt = 0, index = len - 1; index >= mid, cnt < (len - 2) / 2; index --) {
    					if (index != i && index != i - 1) {
    						y = y * 10 + nums[index];
    						cnt ++;
    					}
    				}
    				if (ans > (x - y)) ans = x - y;
    			}
    		}
    		return ans;
    	}	
    }
    
    int main() {
    	int t;
    	scanf("%d", &t);
    	while (t --) {
    		printf("%d
    ", solve());
    	}
    	return 0;
    }  
    
  • 相关阅读:
    最近有人说我欺骗消费者,今天来一波视频分享
    前端 Java Python等资源合集大放送
    dubbo源码学习(四):暴露服务的过程
    dubbo源码学习(二) : spring 自定义标签
    Dubbo多注册中心和Zookeeper服务的迁移
    线程各种状态转换分析
    java并发之同步辅助类CountDownLatch
    工作5年的Java程序员,才学会阅读源码,可悲吗?
    【阿里面试系列】Java线程的应用及挑战
    「阿里面试系列」搞懂并发编程,轻松应对80%的面试场景
  • 原文地址:https://www.cnblogs.com/CSLaker/p/7281207.html
Copyright © 2011-2022 走看看