題目鏈接:
http://www.topcoder.com/stat?c=problem_statement&pm=10154&rd=13694看了解題報(bào)告才做出來,實(shí)在是太水了.T.T
關(guān)鍵的思路在于無論怎么開關(guān),相同的行永遠(yuǎn)是相同的。
此外就是在某一開關(guān)上開關(guān)偶數(shù)次等于0次,開關(guān)的順序是無關(guān)的。
對于某一行,把它變成全1所需要的操作次數(shù)為它的0的個數(shù)。
而這些操作最終導(dǎo)致全1的行數(shù)等于最初與這一行相同的行的行數(shù)。
用map存儲一下每一行相同的行數(shù),再看k次開關(guān)能否使其變成全1。
能使其變成全1的話,即可更新一下最大的行數(shù)了。
#include <vector>
#include <algorithm>
#include <sstream>
#include <string>
#include <iostream>
#include <map>
using namespace std;
class LampsGrid
{
public:
int mostLit(vector <string> initial, int K)
{
map<string,int> cnt;
for(int i=0;i<initial.size();++i){
cnt[initial[i]]++;
}
int res = 0;
for(map<string,int>::iterator i = cnt.begin();
i!=cnt.end();
++i){
int num_0 = count(i->first.begin(),i->first.end(),'0');
if( (K>=num_0) && (K-num_0)%2==0 ){
res = max(res,cnt[i->first]);
}
}
return res;
}
}