UVa Problem 10142 Australian Voting (澳大利亚投票)

  1. // Australian Voting (澳大利亚投票)   
  2. // PC/UVa IDs: 110108/10142, Popularity: B, Success rate: low Level: 1   
  3. // Verdict: Accepted   
  4. // Submission Date: 2011-05-22   
  5. // UVa Run Time: 0.844s   
  6. //   
  7. // 版权所有(C)2011,邱秋。metaphysis # yeah dot net   
  8.       
  9. #include <iostream>   
  10. #include <sstream>   
  11. #include <vector>   
  12. #include <iterator>   
  13.       
  14. using namespace std;  
  15.       
  16. void voting(int current, int ncandidates, vector < string > &candidate,  
  17.             vector < int > &vote, vector < vector < int > > &choice)  
  18. {  
  19.     // 统计首选候选人的选票数量。   
  20.     for (int c = 0; c < choice.size(); c++)  
  21.         vote[choice[c][0] - 1]++;  
  22.       
  23.     int nchoices = choice.size();       // 选票总数。   
  24.     while (1)  
  25.     {  
  26.         bool tied = true;   // 候选人票数是否相同。   
  27.         int min = 1000;     // 用于寻找选票最少的候选人,最多1000张选票。   
  28.         int first = -1;  
  29.         for (int c = 0; c < ncandidates; c++)  
  30.         {  
  31.             // 如果有某人票数过半,则判定此人赢得选举,否则继续统计选票。   
  32.             if (vote[c] > (nchoices / 2))  
  33.             {  
  34.                 cout << candidate[c] << endl;  
  35.                 return;  
  36.             }  
  37.               
  38.             // 得到未出局第一个候选人的票数。   
  39.             if (vote[c] >= 0 && first == -1)  
  40.                 first = vote[c];  
  41.                   
  42.             // 若其他候选人与当前找到的未出局候选人票数不同,不是平局。   
  43.             if (first >=0 && vote[c] >= 0 && vote[c] != first)  
  44.                 tied = false;  
  45.                   
  46.             // 查找选票最少的候选人。   
  47.             if (vote[c] >= 0 && vote[c] < min)  
  48.                 min = vote[c];  
  49.         }  
  50.       
  51.         // 检查所有候选人是否票数相同。   
  52.         if (tied)  
  53.         {  
  54.             for (int i = 0; i < ncandidates; i++)  
  55.                 if (vote[i] >= 0)  
  56.                     cout << candidate[i] << endl;  
  57.             return;  
  58.         }  
  59.           
  60.         // 根据min值剔除选票最少的候选人及其在选票的位置。   
  61.         for (int i = 0; i < choice.size(); i++)  
  62.         {  
  63.             // 是否需要重新计算该张选票。   
  64.             bool recount = vote[choice[i][0] - 1] == min;  
  65.             for (int j = choice[i].size() - 1; j >= 0; j--)  
  66.                 if (vote[choice[i][j] - 1] == min)  
  67.                     choice[i].erase(choice[i].begin() + j);  
  68.             if (recount)  
  69.                 vote[choice[i][0] - 1]++;  
  70.         }  
  71.           
  72.         // 选票数为-1表示出局。         
  73.         for (int n = 0; n < ncandidates; n++)  
  74.             if (vote[n] == min)  
  75.                 vote[n] = -1;  
  76.     }  
  77. }  
  78.       
  79. int main(int ac, char *av[])  
  80. {  
  81.     vector < string > candidate;      // 候选人。   
  82.     vector < int > vote;          // 候选人所得选票数。   
  83.     vector < vector < int > > choice;   // 记录选票。   
  84.     int cases;              // 测试数据组数。   
  85.     int current = 0;            // 当前处理的数据组数。   
  86.     int ncandidates;            // 每组数据候选人总数。   
  87.     string line;                // 读入数据用。   
  88.       
  89.     // 读取测试数据组数。   
  90.     cin >> cases;  
  91.       
  92.     // 读取各组数据。   
  93.     while (current < cases)  
  94.     {  
  95.         cin >> ncandidates;  
  96.       
  97.         // 候选人个数是否大于0。   
  98.         if (ncandidates > 0)  
  99.         {  
  100.             cin.ignore();  
  101.               
  102.             candidate.clear();  
  103.             candidate.resize(ncandidates);  
  104.   
  105.             // 读取候选人列表。   
  106.             for (int i = 0; i < ncandidates; i++)  
  107.             {  
  108.                 getline(cin, line);  
  109.                 candidate[i] = line;  
  110.             }  
  111.       
  112.             // 读取选票。   
  113.             choice.clear();  
  114.             while((getline(cin, line), line.length() > 0))  
  115.             {  
  116.                 istringstream iss(line);  
  117.                 vector < int > tmp;  
  118.                 tmp.resize(ncandidates);  
  119.                 for (int i = 0; i < ncandidates; i++)  
  120.                     iss >> tmp[i];  
  121.                 choice.push_back(tmp);  
  122.             }  
  123.               
  124.             // 初始化候选人的选票数为0。   
  125.             vote.clear();  
  126.             vote.resize(ncandidates);  
  127.             fill(vote.begin(), vote.end(), 0);  
  128.               
  129.             // 模拟选票统计过程。   
  130.             voting(current, ncandidates, candidate, vote, choice);  
  131.         }  
  132.       
  133.         current++;  
  134.           
  135.         // 处理输入的空行。   
  136.         if (current < cases && ncandidates > 0)  
  137.             cout << '\n';  
  138.     }  
  139.       
  140.     return 0;  
  141. }  
// Australian Voting (澳大利亚投票)
// PC/UVa IDs: 110108/10142, Popularity: B, Success rate: low Level: 1
// Verdict: Accepted
// Submission Date: 2011-05-22
// UVa Run Time: 0.844s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net
	
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>
	
using namespace std;
	
void voting(int current, int ncandidates, vector < string > &candidate,
			vector < int > &vote, vector < vector < int > > &choice)
{
	// 统计首选候选人的选票数量。
	for (int c = 0; c < choice.size(); c++)
		vote[choice[c][0] - 1]++;
	
	int nchoices = choice.size();		// 选票总数。
	while (1)
	{
		bool tied = true;	// 候选人票数是否相同。
		int min = 1000;		// 用于寻找选票最少的候选人,最多1000张选票。
		int first = -1;
		for (int c = 0; c < ncandidates; c++)
		{
			// 如果有某人票数过半,则判定此人赢得选举,否则继续统计选票。
			if (vote[c] > (nchoices / 2))
			{
				cout << candidate[c] << endl;
				return;
			}
			
			// 得到未出局第一个候选人的票数。
			if (vote[c] >= 0 && first == -1)
				first = vote[c];
				
			// 若其他候选人与当前找到的未出局候选人票数不同,不是平局。
			if (first >=0 && vote[c] >= 0 && vote[c] != first)
				tied = false;
				
			// 查找选票最少的候选人。
			if (vote[c] >= 0 && vote[c] < min)
				min = vote[c];
		}
	
		// 检查所有候选人是否票数相同。
		if (tied)
		{
			for (int i = 0; i < ncandidates; i++)
				if (vote[i] >= 0)
					cout << candidate[i] << endl;
			return;
		}
		
		// 根据min值剔除选票最少的候选人及其在选票的位置。
		for (int i = 0; i < choice.size(); i++)
		{
			// 是否需要重新计算该张选票。
			bool recount = vote[choice[i][0] - 1] == min;
			for (int j = choice[i].size() - 1; j >= 0; j--)
				if (vote[choice[i][j] - 1] == min)
					choice[i].erase(choice[i].begin() + j);
			if (recount)
				vote[choice[i][0] - 1]++;
		}
		
		// 选票数为-1表示出局。		
		for (int n = 0; n < ncandidates; n++)
			if (vote[n] == min)
				vote[n] = -1;
	}
}
	
int main(int ac, char *av[])
{
	vector < string > candidate;		// 候选人。
	vector < int > vote;			// 候选人所得选票数。
	vector < vector < int > > choice;	// 记录选票。
	int cases;				// 测试数据组数。
	int current = 0;			// 当前处理的数据组数。
	int ncandidates;			// 每组数据候选人总数。
	string line;				// 读入数据用。
	
	// 读取测试数据组数。
	cin >> cases;
	
	// 读取各组数据。
	while (current < cases)
	{
		cin >> ncandidates;
	
		// 候选人个数是否大于0。
		if (ncandidates > 0)
		{
			cin.ignore();
			
			candidate.clear();
			candidate.resize(ncandidates);

			// 读取候选人列表。
			for (int i = 0; i < ncandidates; i++)
			{
				getline(cin, line);
				candidate[i] = line;
			}
	
			// 读取选票。
			choice.clear();
			while((getline(cin, line), line.length() > 0))
			{
				istringstream iss(line);
				vector < int > tmp;
				tmp.resize(ncandidates);
				for (int i = 0; i < ncandidates; i++)
					iss >> tmp[i];
				choice.push_back(tmp);
			}
			
			// 初始化候选人的选票数为0。
			vote.clear();
			vote.resize(ncandidates);
			fill(vote.begin(), vote.end(), 0);
			
			// 模拟选票统计过程。
			voting(current, ncandidates, candidate, vote, choice);
		}
	
		current++;
		
		// 处理输入的空行。
		if (current < cases && ncandidates > 0)
			cout << '\n';
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值