
題意: 給三個長度為10的二進制字串,對其中一個字串里面的數字你可以隨意調動,求字串進行異或處理,求可以得到的最大值
1 ^ 1 ^ 1=1,1 ^ 0 ^ 0=1.只有這兩種能得到1,因為要求取得的值盡可能的大一些,所以我們盡可能的將1排到前面,因為我們要得到更多的1,所以我們先用1 0 0組合,如果不能用1 0 0 再看能否用1 1 1,若都不能,則該位置是0,
#include<bits/stdc++.h>
using namespace std;
struct node
{
int n1=0;
int n2=0;
};
bool cmp(node a,node b)
{
return a.n1>b.n1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string s1,s2,s3;
cin>>s1>>s2>>s3;
node a[4];
for(int i=0;i<s1.size();i++)
if(s1[i]=='1')
a[1].n1++;
else
a[1].n2++;
for(int i=0;i<s2.size();i++)
if(s2[i]=='1')
a[2].n1++;
else
a[2].n2++;
for(int i=0;i<s3.size();i++)
if(s3[i]=='1')
a[3].n1++;
else
a[3].n2++;
for(int i=0;i<10;i++)
{
sort(a+1,a+4,cmp);
if(a[1].n1>0&&a[2].n2>0&&a[3].n2>0)
{
cout<<1;
a[1].n1--;
a[2].n2--;
a[3].n2--;
}
else if(a[1].n1>0&&a[2].n1>0&&a[3].n1>0)
{
cout<<1;
a[1].n1--;
a[2].n1--;
a[3].n1--;
}
else
{
cout<<0;
}
}
cout<<endl;
}
return 0;
}

題意: 給你一個n×m的表格,求每兩層之間元素相同的個數,
記憶化搜查,用一個map存入一層,再回圈查詢存入層的下一層,之后清楚map再輸入查詢層,以此類推(只用cincout會超時,因此要加上快讀)
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
long long a[N][N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
int res=0;
map<int,int >ma;
for(int j=0;j<m;j++)
ma[a[0][j]]++;
for(int i=1;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(ma[a[i][j]])
{
res++;
ma[a[i][j]]--;
}
}
ma.clear();
for(int j=0;j<m;j++)
{
ma[a[i][j]]++;
}
}
cout<<res<<endl;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/267402.html
標籤:其他
