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;
    }  
    
  • 相关阅读:
    微服务架构总结
    微服务-网关服务
    HttpClient-RestTemplate-Feign
    RPC和REST
    Springmvc的拦截器执行顺序及各方法作用
    秒杀系统优化方案(下)吐血整理
    秒杀系统优化方案(上)吐血整理
    分布式session的管理
    缓存设计——缓存和数据库的数据一致性
    个人理解的javascript作用域链与闭包
  • 原文地址:https://www.cnblogs.com/CSLaker/p/7281207.html
Copyright © 2011-2022 走看看