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;
    }  
    
  • 相关阅读:
    opengl 4.5离线文档下载
    c++下利用URLOpenStream下载
    实时阴影渲染(三):软阴影深度校正
    实时阴影渲染(二):软阴影
    实时阴影渲染(一):PSSM平行分割阴影图
    Ogre材质shader模版
    本地springboot项目连接本地mysql报错。com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
    Caused by: java.lang.IllegalStateException: AnnotationAwareAspectJAutoProxyCreator is only available on Java 1.5 and higher
    关于布隆过滤器
    CSS系列------选择器和选择器的优先级
  • 原文地址:https://www.cnblogs.com/CSLaker/p/7281207.html
Copyright © 2011-2022 走看看