T89359 扫雷
T89359 扫雷
题解
朴素做法:暴力出奇迹
一维数组按道理不能开到1e7这么大吧,但是我开了井然 A 了 或许是rp问题
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<queue> using namespace std; #define ll long long inline int read() { int ans=0; char last=' ',ch=getchar(); while(ch<'0'||ch>'9') last=ch,ch=getchar(); while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar(); if(last=='-') ans=-ans; return ans; } int n,k; long long ans=0; int a[10000010]; bool cmp(int x,int y) { return x>y; } int main() { n=read(); if(n%2==0) { printf("-1"); return 0; } for(int i=1;i<=n;i++) { a[i]=read(); } sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i+=2) { if(a[i]!=a[i+1]){ cout<<a[i]; return 0; } } return 0; }
脑洞做法:^
^ 满足交换律,把所有数字 ^ 起来,重复的数字就消失了,最后剩下的数字就是只出 现一次的数字
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<queue> using namespace std; #define ll long long inline int read() { int ans=0; char last=' ',ch=getchar(); while(ch<'0'||ch>'9') last=ch,ch=getchar(); while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar(); if(last=='-') ans=-ans; return ans; } int n,k,ans=0; int main() { n=read(); for(int i=1;i<=n;i++) { k=read(); if(i==1) ans=k; else ans=ans^k; } printf("%d",ans); return 0; }