反正我暴力枚举过了。。有一点比较蛋疼就是要用map记录一下各数出现次数的奇偶,发现奇就直接输出0,全是偶的话再暴力枚举,否则超时。讲真这种技巧真的得学会才行,否则很多题全都做对了但就差这一步没过太可惜了。
用了STL的list
代码
#include<stdio.h>
#include<list>
#include<map>
using namespace std;
list<int>l;
map<int,int>MAP;
int ok()
{
if(l.size())
{
list<int>::iterator it=l.begin();
int cnt=1;
it++;
while(1)
{
while(it!=l.end()&&cnt<=6&&*it!=*l.begin())
{
++it;
++cnt;
}
if(it==l.end()||cnt==7) return 0;
else
{
int f=*l.begin();
int b=*it;
l.erase(it);
l.erase(l.begin());
if(ok()) return 1;
else
{
l.push_front(f);
it=l.begin();
for(int i=0;i<cnt;i++)
it++;
l.insert(it,b);
it=l.begin();
cnt++;
for(int i=0;i<cnt;i++)
it++;
}
}
}
}
else return 1;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
l.clear();
MAP.clear();
int temp;
for(int i=1;i<=n;i++)
{
scanf("%d",&temp);
MAP[temp]++;
l.push_front(temp);
}
map<int,int>::iterator it=MAP.begin();
bool OK=true;
for(;it!=MAP.end();it++) if(it->second%2) {OK=false;break;}
if(OK) printf("%d\n",ok());
else puts("0");
}
return 0;
}
本文介绍了一种使用暴力枚举结合Map记录元素出现次数的方法来解决特定算法问题的技巧。通过判断各数出现次数的奇偶性来优化搜索过程,避免不必要的全量遍历,从而提高效率。
310

被折叠的 条评论
为什么被折叠?



