UVA - 10487 Closest Sums(二分查找)

Problem D
Closest Sums
Input: standard input
Output: standard output
Time Limit: 3 seconds

 

Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct numbers from the set, which is closest to the query number.

Input

Input contains multiple cases.

Each case starts with an integer n (1<n<=1000), which indicates, how many numbers are in the set of integer. Next n lines contain n numbers. Of course there is only one number in a single line. The next line contains a positive integer m giving the number of queries, 0 < m < 25. The next m lines contain an integer of the query, one per line.

Input is terminated by a case whose n=0. Surely, this case needs no processing.

Output

Output should be organized as in the sample below. For each query output one line giving the query value and the closest sum in the format as in the sample. Inputs will be such that no ties will occur.

Sample input

5

3 
12 
17 
33 
34 
3 
1 
51 
30 
3 
1 
2 
3 
3 
1 
2 
3 

3

1 
2 
3 
3 
4 
5 
6 
0 

Sample output

Case 1:     
Closest sum to 1 is 15.     
Closest sum to 51 is 51.     
Closest sum to 30 is 29.     
Case 2:     
Closest sum to 1 is 3.     
Closest sum to 2 is 3.     
Closest sum to 3 is 3.     
Case 3:     
Closest sum to 4 is 4.     
Closest sum to 5 is 5.     
Closest sum to 6 is 5.     


题目:给你n个数字set[1~n],以及m个数字query[1-m],对于每个query[i]找到对应的两个set[j],set[k]使得他们的和最接近query[i]。

解析:用二分求解,比较简单。详见代码。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N = 1005;
const int INF = 0x3f3f3f3f;
int set[N];
int query[N];
int n,m;
int solve(int num) { //二分查找最近的和
	int left = 0,right = n-1;
	int sum,dis;
	int min = INF,ans;
	while(left < right) {
		sum = set[left] + set[right];
		dis = abs(sum - num);
		if(dis < min) {
			min = dis;
			ans = sum;
		}
		if(sum == num) {
			return num;
		}else if(sum < num) {
			left++;
		}else {
			right--;
		}
	}
	return ans;
}
int main() {
	int cas = 1;
	while(scanf("%d",&n) != EOF && n) {
		for(int i = 0; i < n; i++) {
			scanf("%d",&set[i]);
		}
		sort(set,set+n);
		scanf("%d",&m);
		for(int i = 0; i < m; i++) {
			scanf("%d",&query[i]);
		}
		printf("Case %d:\n",cas++);
		for(int i = 0; i < m; i++) {
			int ans = solve(query[i]);
			printf("Closest sum to %d is %d.\n",query[i],ans);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二分K均值是一种基于K均值算法的改进方法,用于图像分割时可以得到更好的结果。 实现步骤如下: 1. 将图像转化为一维数组。 2. 设定初始的K个聚类中心(可以随机选择或使用其他方法),计算每个像素点与聚类中心的距离,并将其归入距离最近的类别中。 3. 计算每个类别的平均值作为新的聚类中心。 4. 重复步骤2和步骤3,直到聚类中心不再发生变化或达到设定的迭代次数。 5. 对于二分K均值,将所有样本归入一个初始类别中,然后将其分成两个子类别。对每一个子类别重复1-4步骤,直到达到设定的最大类别数或某些停止条件。 6. 最终将每个像素点归入距离最近的聚类中心所在的类别中,即可得到图像分割结果。 下面是Python代码实现: ```python import numpy as np from PIL import Image def binary_kmeans(image_path, k_max=10, max_iter=20): # Load image image = Image.open(image_path) pixels = np.array(image).reshape(-1, 3) # Initialize with one cluster clusters = [(0, pixels)] # Binary K-means loop while len(clusters) < k_max: # Choose the cluster with maximum SSE sse_list = [np.sum((cluster[1] - np.mean(cluster[1], axis=0)) ** 2) for cluster in clusters] max_sse_idx = np.argmax(sse_list) max_sse_cluster = clusters[max_sse_idx] # Split the cluster into two sub-clusters kmeans = KMeans(n_clusters=2, max_iter=max_iter).fit(max_sse_cluster[1]) sub_clusters = [(max_sse_cluster[0] * 2 + i, max_sse_cluster[1][kmeans.labels_ == i]) for i in range(2)] # Replace the original cluster with the sub-clusters clusters = clusters[:max_sse_idx] + sub_clusters + clusters[max_sse_idx + 1:] # Assign each pixel to the closest cluster labels = np.zeros(len(pixels), dtype=int) for cluster in clusters: labels[np.argmin(np.sum((pixels - np.mean(cluster[1], axis=0)) ** 2, axis=1))] = cluster[0] # Reshape the labels back to an image return labels.reshape(image.size[1], image.size[0]) ``` 其中,`image_path`为需要分割的图像路径,`k_max`为最大聚类数,`max_iter`为每个子类别的最大迭代次数。该函数返回每个像素点所属的类别标签,可以使用不同的颜色来可视化分割结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值