• 算法训练 审美课


    本文由 简悦 SimpRead 转码, 原文地址 http://www.manongjc.com/article/68929.html

    算法训练 审美课

    时间限制:1.0s 内存限制:256.0MB
    提交此题
    问题描述
      《审美的历程》课上有 n 位学生,帅老师展示了 m 幅画,其中有些是梵高的作品,另外的都出自五岁小朋友之手。老师请同学们分辨哪些画的作者是梵高,但是老师自己并没有答案,因为这些画看上去都像是小朋友画的…… 老师只想知道,有多少对同学给出的答案完全相反,这样他就可以用这个数据去揭穿披着皇帝新衣的抽象艺术了(支持帅老师_)。
      答案完全相反是指对每一幅画的判断都相反。
    输入格式
      第一行两个数 n 和 m,表示学生数和图画数;
      接下来是一个 n*m 的 01 矩阵 A:
      如果 aij=0,表示学生 i 觉得第 j 幅画是小朋友画的;
      如果 aij=1,表示学生 i 觉得第 j 幅画是梵高画的。
    输出格式
      输出一个数 ans:表示有多少对同学的答案完全相反。
    样例输入
    3 2
    1 0
    0 1
    1 0
    样例输出
    2
    样例说明
      同学 1 和同学 2 的答案完全相反;
      同学 2 和同学 3 的答案完全相反;
      所以答案是 2。
    数据规模和约定
      对于 50% 的数据:n<=1000;
      对于 80% 的数据:n<=10000;
      对于 100% 的数据:n<=50000,m<=20。

    二进制和位运算的巧用

    #include <iostream>
    using namespace std;
    int g_Binary[50001];//g_Binary[i],同学选择的二进制代表的数 
    int g_Ans[1050000];//g_Ans[i],相同选择的同学的个数 
    /*
    3 2
    1 0
    0 1
    1 0
    */ 
    int main(void)
    {
    	int n, k;
    	cin >> n >> k;
    	for (int i = 0; i < n; i++)
    	{
    		for (int j = 0; j < k; j++)
    		{
    			int temp;
    			cin >> temp;//temp为1,0 
    			g_Binary[i] = (g_Binary[i] << 1) + temp;//第i同学选择的二进制数代表的数 
    		}
    		g_Ans[g_Binary[i]]++;//相同二进制的同学(相同选择的同学)存在一起 
    		//分析案例,g_Ans[2] = 2, g_Ans[1] = 1; 
    	}
    
    	int max = (1 << k) - 1;//分析案例 max = 0011 = 3;
    	int sum = 0;
    
    	for (int i = 0; i < n; i++)
    	{
    		int temp = g_Binary[i] ^ max;//同学选择的二进制与max按位取反,得到完全相反的选择 
    		//temp = g_Ans[1] ^ max = 1 ^ 3 = 0001 ^ 0011 = 0010 = 2  
    		sum += g_Ans[temp];//与g_Ans[1]相反的同学的个数就是g_Ans[2] 
    	} 
    
    	cout << sum / 2 << endl;//要求取学生的对数 
    	return 0;
    }
    
    

    上一页 下一页

    原文地址:http://www.manongjc.com/article/68929.html

    我的思路

    我想的和上面的博客的思路是一样的,我一开始也想到了二进制,然后计算数字部分没考虑清楚,后面换成了字符串hash,存在map中。

    My Code:

    /*
    题目链接:http://lx.lanqiao.cn/problem.page?gpid=T519
    Author: coolMarlon 
    My Email:shengwangshi@qq.com
    */
    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    #define maxn 50005
    #define maxm 21
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int A[maxn][maxm];
    
    string my_reverse(string str)
    {
    	string res="";
    	for(int i=0;i<str.size();i++)
    	{
    		res=res+(char)('0'+!(str[i]-'0'));
    	}
    	return res;
    }
    
    int main(int argc, char *argv[]) {
    	//cout<<"hello World" <<endl; 
    	int n,m;
    	cin>>n>>m;
    	map<string,int> iimap;
    	int temp;
    	for(int i=0;i<n;i++)
    	{
    		string temp_str="";
    		for(int j=0;j<m;j++)
    		{
    			cin>>temp;
    			temp_str=temp_str+(char)('0'+temp);
    		}
    		iimap[temp_str]++;		
    	}
    	map<string,int> comp_map;
    	int cnt=0;
    	for(map<string,int>::iterator it=iimap.begin();it!=iimap.end();it++)
    	{
    		string temp_str=my_reverse(it->first);
    		//cout<<temp_str<<endl;
    		cnt+=(iimap[temp_str]*iimap[it->first]);
    	}
    	cout<<cnt/2;
    	return 0;
    }
    
  • 相关阅读:
    第七次作业
    随堂讨论8
    第六次作业
    随堂讨论-5
    曹宇轩-第五次作业
    随堂讨论3-刘昕昕,曹宇轩
    曹宇轩-第四次作业
    Alpha阶段项目复审
    团队作业4 -项目冲刺
    第六篇 Scrum冲刺博客
  • 原文地址:https://www.cnblogs.com/shengwang/p/10512957.html
Copyright © 2020-2023  润新知