A - Buy the String
要么全变成1要么全变成0要么一个都不改变,三种情况取最小。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int main()
{
IO;
int T=1;
cin>>T;
while(T--)
{
int n;
cin>>n;
int a,b,c;
cin>>a>>b>>c;
string s;
cin>>s;
int res1=0;
for(int i=0;i<n;i++)
{
if(s[i]=='0') res1+=a;
else res1+=b;
}
int res2=0;
for(int i=0;i<n;i++)
{
if(s[i]=='0') res2+=b+c;
else res2+=b;
}
int res3=0;
for(int i=0;i<n;i++)
{
if(s[i]=='0') res3+=a;
else res3+=a+c;
}
cout<<min(res1,min(res2,res3))<<'\n';
}
return 0;
}
B - Sum of Medians
数学题,取后面几个中位数即可
任意考虑一个不降序列
a
1
a
2
a
3
…
a
n
a_1 \ a_2 \ a_3\dots a_n
a1 a2 a3…an,按照题目意思中位数为
a
⌊
1
+
n
2
⌋
a_{\lfloor \frac{1+n}{2}\rfloor}
a⌊21+n⌋,我们那么中位数后面的数的个数为
n
−
⌊
1
+
n
2
⌋
n-\lfloor \frac{1+n}{2}\rfloor
n−⌊21+n⌋意味着如果你选择一个数作为中位数那么一定要有
n
−
⌊
1
+
n
2
⌋
n-\lfloor \frac{1+n}{2}\rfloor
n−⌊21+n⌋不小于它的数。
那么本题只需要每次选择第 n − ⌊ 1 + n 2 ⌋ + 1 n-\lfloor \frac{1+n}{2}\rfloor+1 n−⌊21+n⌋+1大的数作为每个数组的中位数即可。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1000010;
int n,k;
ll a[N];
int main()
{
IO;
int T=1;
cin>>T;
while(T--)
{
cin>>n>>k;
for(int i=1;i<=n*k;i++)
{
a[i]=0;
cin>>a[i];
}
sort(a+1,a+1+n*k);
int m=(n+1)/2;
int cnt=n-m;
ll res=0;
for(int i=n*k,j=1;i>=1,j<=k;j++,i-=cnt+1)
res+=a[i-cnt];
cout<<res<<'\n';
}
return 0;
}
C1 - Binary Table (Easy Version)
[
1
1
1
1
]
→
[
1
0
0
0
]
→
[
1
1
0
0
]
→
[
1
1
1
0
]
→
[
0
0
0
0
]
\begin{bmatrix}1 &1\\ 1&1\end{bmatrix}\to \begin{bmatrix}1 &0\\ 0&0\end{bmatrix}\to \begin{bmatrix}1 &1\\ 0&0\end{bmatrix} \to \begin{bmatrix}1 &1\\ 1&0\end{bmatrix} \to \begin{bmatrix}0 &0\\ 0&0\end{bmatrix}
[1111]→[1000]→[1010]→[1110]→[0000]
每次考虑变换4个格子,每4个格子最多变换4次,我们会把4个数全部变成0,如果行列都是偶数,那么保证mn次操作内绝对能够全变成0
由于行列奇偶行可能导致出现某些元素重复变换导致nm次过不了(这也是更难的版本
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=110;
int n,m;
char tmp[N][N];
int g[N][N];
pii a[4];
bool st[4];
vector<pii> ans;
void work(pii a[])
{
memset(st,0,sizeof st);
int cnt=0;
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
cnt+=g[x][y];
}
if(cnt==1)
{
int now=0;
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
if(now<2&&!g[x][y])
{
st[i]=1;
ans.push_back({x,y});
g[x][y]=1;
now++;
}
}
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
if(!st[i]&&g[x][y])
{
ans.push_back({x,y});
g[x][y]=0;
break;
}
}
work(a);
}
else if(cnt==2)
{
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
if(!g[x][y])
{
st[i]=1;
ans.push_back({x,y});
g[x][y]=1;
}
}
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
if(!st[i]&&g[x][y])
{
ans.push_back({x,y});
g[x][y]=0;
break;
}
}
work(a);
}
else if(cnt==3)
{
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
if(g[x][y])
{
ans.push_back({x,y});
g[x][y]=0;
}
}
}
else if(cnt==4)
{
for(int i=0;i<3;i++)
{
int x=a[i].first,y=a[i].second;
if(g[x][y])
{
ans.push_back({x,y});
g[x][y]=0;
}
}
work(a);
}
else return;
}
int main()
{
IO;
int T=1;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>tmp[i]+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
g[i][j]=tmp[i][j]-'0';
for(int i=1;i<=n;i+=2)
for(int j=1;j<=m;j+=2)
{
if(i+1<=n&&j+1<=m)
a[0]={i,j},a[1]={i+1,j},a[2]={i,j+1},a[3]={i+1,j+1};
else if(i+1>n&&j+1<=m)
a[0]={i,j},a[1]={i-1,j},a[2]={i,j+1},a[3]={i-1,j+1};
else if(i+1<=n&&j+1>m)
a[0]={i,j},a[1]={i+1,j},a[2]={i,j-1},a[3]={i+1,j-1};
else
a[0]={i,j},a[1]={i-1,j},a[2]={i,j-1},a[3]={i-1,j-1};
work(a);
}
cout<<ans.size()/3<<'\n';
int cnt=0;
for(auto t:ans)
{
cnt++;
cout<<t.first<<' '<<t.second<<' ';
if(cnt%3==0) cout<<'\n';
}
ans.clear();
}
return 0;
}
C2 - Binary Table (Hard Version)
对于行列数是奇数可能导致nm操作不能搞的情况:其实我们只要首先把一行全部变成0(行数是奇数)或者一列全部变成0(列数是奇数),我们就能把操作次数控制在 n 2 + 1 + m 2 + 1 + ( m − 1 ) × ( n − 1 ) \frac{n}{2}+1+\frac{m}{2}+1+(m-1)×(n-1) 2n+1+2m+1+(m−1)×(n−1)次以内
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=110;
int n,m;
char tmp[N][N];
int g[N][N];
pii a[4];
bool st[4];
vector<pii> ans;
void work(pii a[])
{
memset(st,0,sizeof st);
int cnt=0;
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
cnt+=g[x][y];
}
if(cnt==1)
{
int now=0;
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
if(now<2&&!g[x][y])
{
st[i]=1;
ans.push_back({x,y});
g[x][y]=1;
now++;
}
}
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
if(!st[i]&&g[x][y])
{
ans.push_back({x,y});
g[x][y]=0;
break;
}
}
work(a);
}
else if(cnt==2)
{
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
if(!g[x][y])
{
st[i]=1;
ans.push_back({x,y});
g[x][y]=1;
}
}
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
if(!st[i]&&g[x][y])
{
ans.push_back({x,y});
g[x][y]=0;
break;
}
}
work(a);
}
else if(cnt==3)
{
for(int i=0;i<4;i++)
{
int x=a[i].first,y=a[i].second;
if(g[x][y])
{
ans.push_back({x,y});
g[x][y]=0;
}
}
}
else if(cnt==4)
{
for(int i=0;i<3;i++)
{
int x=a[i].first,y=a[i].second;
if(g[x][y])
{
ans.push_back({x,y});
g[x][y]=0;
}
}
work(a);
}
else return;
}
void print()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) cout<<g[i][j]<<' ';
cout<<endl;
}
}
int main()
{
IO;
int T=1;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>tmp[i]+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
g[i][j]=tmp[i][j]-'0';
if(n%2==0&&m%2==0)
{
for(int i=1;i<=n;i+=2)
for(int j=1;j<=m;j+=2)
{
a[0]={i,j},a[1]={i+1,j},a[2]={i,j+1},a[3]={i+1,j+1};
work(a);
}
}
else if(n%2==0&&m%2!=0)
{
for(int i=1;i<=n;i+=2)
{
if(g[i][1]==0&&g[i+1][1]==0) continue;
else if(g[i][1]==1&&g[i+1][1]==0)
{
ans.push_back({i,1}); g[i][1]=1-g[i][1];
ans.push_back({i,2}); g[i][2]=1-g[i][2];
ans.push_back({i+1,2}); g[i+1][2]=1-g[i+1][2];
}
else if(g[i][1]==0&&g[i+1][1]==1)
{
ans.push_back({i+1,1}); g[i+1][1]=1-g[i+1][1];
ans.push_back({i,2}); g[i][2]=1-g[i][2];
ans.push_back({i+1,2}); g[i+1][2]=1-g[i+1][2];
}
else
{
ans.push_back({i,1}); g[i][1]=1-g[i][1];
ans.push_back({i+1,1}); g[i+1][1]=1-g[i+1][1];
ans.push_back({i+1,2}); g[i+1][2]=1-g[i+1][2];
}
}
for(int i=1;i<=n;i+=2)
for(int j=2;j<=m;j+=2)
{
a[0]={i,j},a[1]={i+1,j},a[2]={i,j+1},a[3]={i+1,j+1};
work(a);
}
}
else if(n%2!=0&&m%2==0)
{
for(int j=1;j<=m;j+=2)
{
if(g[1][j]==0&&g[1][j+1]==0) continue;
else if(g[1][j]==1&&g[1][j+1]==0)
{
ans.push_back({1,j});g[1][j]=1-g[1][j];
ans.push_back({2,j});g[2][j]=1-g[2][j];
ans.push_back({2,j+1});g[2][j+1]=1-g[2][j+1];
}
else if(g[1][j]==0&&g[1][j+1]==1)
{
ans.push_back({1,j+1});g[1][j+1]=1-g[1][j+1];
ans.push_back({2,j});g[2][j]=1-g[2][j];
ans.push_back({2,j+1});g[2][j+1]=1-g[2][j+1];
}
else
{
ans.push_back({1,j});g[1][j]=1-g[1][j];
ans.push_back({1,j+1});g[1][j+1]=1-g[1][j+1];
ans.push_back({2,j});g[2][j]=1-g[2][j];
}
}
for(int i=2;i<=n;i+=2)
for(int j=1;j<=m;j+=2)
{
a[0]={i,j},a[1]={i+1,j},a[2]={i,j+1},a[3]={i+1,j+1};
work(a);
}
//print();
}
else
{
for(int i=1;i<=n;i+=2)
{
int x=i+1;
if(x>n) x-=2;
if(g[i][1]==0&&g[x][1]==0) continue;
else if(g[i][1]==1&&g[x][1]==0)
{
ans.push_back({i,1}); g[i][1]=1-g[i][1];
ans.push_back({i,2}); g[i][2]=1-g[i][2];
ans.push_back({x,2}); g[x][2]=1-g[x][2];
}
else if(g[i][1]==0&&g[x][1]==1)
{
ans.push_back({x,1}); g[x][1]=1-g[x][1];
ans.push_back({i,2}); g[i][2]=1-g[i][2];
ans.push_back({x,2}); g[x][2]=1-g[x][2];
}
else
{
ans.push_back({x,1}); g[x][1]=1-g[x][1];
ans.push_back({i,1}); g[i][1]=1-g[i][1];
ans.push_back({x,2}); g[x][2]=1-g[x][2];
}
}
for(int j=2;j<=m;j+=2)
{
int y=j+1;
if(y>m) y-=2;
if(g[1][j]==0&&g[1][y]==0) continue;
else if(g[1][j]==1&&g[1][y]==0)
{
ans.push_back({1,j});g[1][j]=1-g[1][j];
ans.push_back({2,j});g[2][j]=1-g[2][j];
ans.push_back({2,y});g[2][y]=1-g[2][y];
}
else if(g[1][j]==0&&g[1][y]==1)
{
ans.push_back({1,y});g[1][y]=1-g[1][y];
ans.push_back({2,j});g[2][j]=1-g[2][j];
ans.push_back({2,y});g[2][y]=1-g[2][y];
}
else
{
ans.push_back({1,j});g[1][j]=1-g[1][j];
ans.push_back({2,j});g[2][j]=1-g[2][j];
ans.push_back({1,y});g[1][y]=1-g[1][y];
}
}
//print();
for(int i=2;i<=n;i+=2)
for(int j=2;j<=m;j+=2)
{
a[0]={i,j},a[1]={i+1,j},a[2]={i,j+1},a[3]={i+1,j+1};
work(a);
}
}
cout<<ans.size()/3<<'\n';
int cnt=0;
for(auto t:ans)
{
cnt++;
cout<<t.first<<' '<<t.second<<' ';
if(cnt%3==0) cout<<'\n';
}
ans.clear();
}
return 0;
}
这模拟就离谱好吧~~
写了4题太晚了(希望不被fst,就没有在往后看
E. Greedy Shopping
今天补了一下E. Greedy Shopping
要加油哦~