本文由 简悦 SimpRead 转码, 原文地址 http://www.manongjc.com/article/68929.html 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。ios
二进制和位运算的巧用算法
#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.htmlthis
我想的和上面的博客的思路是同样的,我一开始也想到了二进制,而后计算数字部分没考虑清楚,后面换成了字符串hash,存在map中。spa
My Code: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; }