2022.3.12
蓝书
AcWing 116. 飞行员兄弟
位运算,枚举每一位把手的状态,是关的就让他开着,记录一下每次的操作和每次的最小次数,和费解的开关那题解法差不多。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
vector<pii> b;
vector<pii> bb;
int a[10][10];
void change(int x,int y)
{
for (int i = 0; i < 4;i++)
{
a[x][i] ^= 1;
a[i][y] ^= 1;
}
a[x][y] ^= 1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 0; i < 4;i++)
for (int j = 0; j < 4;j++)
{
char x;
cin >> x;
if(x=='+')
a[i][j] = 1;
}
int ans = INF;
for (int i = 0; i < 1 << 16;i++)
{
int res = 0;
int back[10][10] = {0};
b.clear();
memcpy(back, a, sizeof a);
for (int j = 0; j < 4;j++)
for (int k = 0; k < 4;k++)
{
if(i>>(j*4+k)&1)
{
change(j, k);
res++;
b.push_back({j,k});
}
}
int ok = 1;
for (int j = 0; j < 4;j++)
{
for (int k = 0; k < 4;k++)
if(a[j][k])
{
ok = 0;
break;
}
if(!ok)
break;
}
if(ok)
{
if(res<ans)
{
bb.clear();
for(auto x:b)
bb.push_back(x);
ans = res;
}
}
memcpy(a, back, sizeof back);
}
cout << ans << endl;
for(auto x:bb)
{
cout << x.first+1 << ' ' << x.second+1<< endl;
}
return 0;
}
AcWing 117. 占卜DIY
按题目要求模拟,输入格式有空格需要注意,要不就读字符串要不就getchar()
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
vector<int> a[15];
int vis[15];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 1; i <= 13;i++)
{
for (int j = 1; j <=4 ;j++)
{
char x;
cin >> x;
getchar();
if(x>='2'&&x<='9')
a[i].push_back( x - '0');
else if(x=='0')
a[i].push_back(10);
else if(x=='A')
a[i].push_back(1);
else if(x=='J')
a[i].push_back(11);
else if(x=='Q')
a[i].push_back(12);
else if(x=='K')
a[i].push_back(13);
}
}
for (int i = 0; i < 4;i++)
{
int t = a[13][i];
while(t!=13)
{
vis[t]++;
int tt = a[t].back();
a[t].pop_back();
t=tt;
}
}
int ans = 0;
for (int i = 1; i <= 12;i++)
{
if(vis[i]==4)
ans++;
}
cout << ans << endl;
return 0;
}
AcWing 118. 分形
思路:对于n>=2,我们只需要得到每个图形左上角的图形,再将其放到指定的位置即可。当n=1时可以直接输出,n>=2时可以通过递归求解。可以发现:对于第1级分形它的长度len=1,第2级为3,第3级为9,因此第n级分形的长度为3^(n-1)。只要通过左上角图形的len确定其他四个部分的坐标,最后输出即可。
#include<bits/stdc++.h>
using namespace std;
const int N=1000;
char g[N][N];
void dfs(int n)
{
if(n==1)
{
g[0][0]='X';//如果只有1级直接返回'X'
return ;
}
dfs(n-1);//递归找它的上一级
int len=1;//确定第n-1级的长度
for(int i=0;i<n-2;i++) len*=3;//输出第n-1级的图形
int sx[4]={0,1,2,2},sy[4]={2,1,0,2};
for(int k=0;k<4;k++)
for(int i=0;i<len;i++)
for(int j=0;j<len;j++)
g[sx[k]*len+i][sy[k]*len+j]=g[i][j];
//确定剩下四个部分的位置就确定了第n级
}
int main()
{
dfs(7);
int n;
while(~scanf("%d",&n)&&n!=-1)
{
int len=1;//找到第n级的长度
for(int i=0;i<n-1;i++) len*=3;
//第1级长度为1,第2级为3,第3级为9....
for(int i=0;i<len;i++)
{
for(int j=0;j<len;j++)
{
if(g[i][j]=='X') printf("X");
else printf(" ");
}
printf("\n");
}
printf("-\n");
}
return 0;
}
CFAB题特训赛2
A - Playoff
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d", &n);
int k = pow(2, n) - 1;
printf("%d\n", k);
}
return 0;
}
B - Prove Him Wrong
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d", &n);
if(n>19)
printf("NO\n");
else
{
printf("YES\n");
int k = 1;
for (int i = 1; i <= n;i++)
{
printf("%d ", k);
k *= 3;
}
printf("\n");
}
}
return 0;
}
C - Fault-tolerant Network
当晚做的时候只画出了几种情况而已,赛后看题解发现是7种情况,分别是连2条边,连3条边,连4条边。对于两排特殊的四个点,即a[1],a[n],,b[1],b[n]。对于连两条边,此时只有a[1]->b[1],a[n]->b[n]。当为3条边的时候,我们任意选择这四条边的其中两条连上,剩下的两条边让他们去连对面任意的点,此时贪心求最小代价。对于4条边就是这4个点任意连,求最小代价。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=2e5+10,INF=1e9;
ll a[N], b[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for (int i = 1; i <= n;i++)
cin >> a[i];
for (int i = 1; i <= n;i++)
cin >> b[i];
ll ans = min(abs(a[1]-b[1]) + abs(a[n]-b[n]), abs(a[1]-b[n]) + abs(a[n]-b[1]));
ll res1 = 1e9, res2 = 1e9, res3 = 1e9, res4 = 1e9;
for (int i = 1; i <= n;i++)
{
res1 = min(res1, abs(a[1] - b[i]));
res2 = min(res2, abs(a[n] - b[i]));
res3 = min(res3, abs(b[1] - a[i]));
res4 = min(res4, abs(b[n] - a[i]));
}
ans = min({ans, res1 + res2 + res3 + res4, abs(a[1] - b[1]) + res2 + res4, abs(a[1] - b[n]) + res2 + res3, abs(b[1] - a[n]) + res1 + res4, abs(b[n] - a[n]) + res1 + res3});
cout << ans << '\n';
}
return 0;
}
D - Boy or Girl
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int s[30];
int main()
{
string a;
cin >> a;
for (int i = 0; i < a.size();i++)
{
s[a[i] - 'a'] = 1;
}
int cnt = 0;
for (int i = 0; i < 30;i++)
{
if(s[i])
cnt++;
}
if(cnt&1)
printf("IGNORE HIM!");
else
printf("CHAT WITH HER!");
return 0;
}
E - Three Pairwise Maximums
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int s[30];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if(x==y&&x==z&&y==z)
{
printf("YES\n");
printf("%d %d %d\n", x, y, z);
}
else if((x==y&&x>z)||(x==z&&x>y)||(y==z&&y>x))
{
printf("YES\n");
if(x==y&&x>z)
{
printf("%d %d 1\n", x, z);
}
else if(x==z&&x>y)
{
printf("%d %d 1\n", x,y);
}
else if(y==z&&y>x)
{
printf("%d %d 1\n", y, x);
}
}
else
{
printf("NO\n");
}
}
return 0;
}
F - New Theatre Square
贪心,我们尽量选花费小的砖铺,对于当前的点看看它下一个是不是一样可以铺,如果可以就铺长和短里花费最小的,不行就短的。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
char s[110][1010];
int main()
{
int t;
cin>>t;
while (t--)
{
int n, m, x, y;
cin >> n >> m >> x >> y;
int ans = 0;
for (int i = 0; i < n;i++)
for (int j = 0; j < m;j++)
cin >> s[i][j];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (s[i][j] == '.')
{
if(s[i][j+1]=='.')
{
ans += min(2 * x, y);
s[i][j + 1] = '*';
}
else
{
ans += x;
s[i][j] = '*';
}
}
}
}
printf("%d\n", ans);
for (int i = 0; i < n;i++)
for (int j = 0; j < m;j++)
s[i][j] = 0;
}
return 0;
}
G - JOE is on TV!
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int s[30];
int main()
{
double n;
double ans = 0;
scanf("%lf", &n);
for (double i = 1; i <= n;i++)
{
ans += 1.0 / i;
}
printf("%lf", ans);
return 0;
}
H - Huge Boxes of Animal Toys
题面看得头皮发麻,缝合怪娃娃好可怕。对于a,b,c,d。只要(a+b)是奇数,那最后肯定是落在a,b之中的,那么这时如果a||d里有娃娃,那最后a肯定有,同理如果b||c有娃娃,那最后b肯定有,当(a+b)%1==0时同理
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2000+10,INF=1e8;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
if((a+b)&1)
{
if(a||d)
{
cout << "Ya ";
}
else
cout << "Tidak ";
if(b||c)
{
cout << "Ya ";
}
else
cout << "Tidak ";
cout << "Tidak Tidak" << '\n';
}
else
{
cout << "Tidak Tidak ";
if(b||c)
{
cout << "Ya ";
}
else
cout << "Tidak ";
if(a||d)
{
cout << "Ya " << endl;
}
else
cout << "Tidak" << endl;
}
}
return 0;
}
I - Repression
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int a[10];
int main()
{
for (int i = 1; i <= 3;i++)
cin >> a[i];
sort(a + 1, a + 1 + 3);
printf("%d", a[3] + a[2]);
return 0;
}
J - Hydrate
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int s[30];
int main()
{
ll a, b, c, d;
scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
ll n = a, m = 0,cnt=0;
for (int i = 1; i <= a;i++)
{
n += b;
m += c;
if(n<=m*d)
{
printf("%d\n", i);
return 0;
}
}
printf("-1");
return 0;
}
K - Many Segments
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2000+10,INF=1e8;
double l[N], r[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,cnt=0;
cin >> n;
for (int i = 1; i <= n;i++)
{
int op;
cin >> op>>l[i]>>r[i];
if(op==2)
{
r[i] -= 0.5;
}
else if(op==3)
{
l[i] += 0.5;
}
else if(op==4)
{
l[i] += 0.5, r[i] -= 0.5;
}
}
for (int i = 1; i < n;i++)
{
for (int j = i + 1; j <= n;j++)
{
double ll=max(l[i],l[j]),rr=min(r[i],r[j]);
if(ll<=rr)
cnt++;
}
}
cout << cnt;
return 0;
}
L - MAX-MEX Cut
当时写的还不完善,后面看了题解发现大佬用dp,但感觉不用dp也能做,因为a[i]和b[i]都为1是0,所以要想办法把它搞大点,看看i-1和i+1有没有0,如果有0的话说明可以合在一起,结果就会+1,没有就没办法啦。走的过程还得记录一下哪些是用过的,不能重复了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
string a, b;
map<int, int> mp;
cin >> a >> b;
ll ans = 0;
for (int i = 0; i <n;i++)
{
if(a[i]!=b[i])
{
ans += 2;
}
else
{
if(a[i]=='0')
ans++;
else if(a[i-1]=='0'&&b[i-1]=='0'&&mp[i-1]==0)
{
mp[i - 1] = 1;
ans ++;
}
else if(a[i+1]=='0'&&b[i+1]=='0'&&mp[i+1]==0)
{
mp[i + 1] = 1;
ans ++;
}
}
}
printf("%lld\n", ans);
}
return 0;
}
AtCoder Beginner Contest 243
A - Shampoo
一开始给这sb题卡了草,真急了,自己是真的睿智写的什么屌代码,重写了一遍
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int v, a, b, c;
cin >> v >> a >> b >> c;
int ans = v % (a + b + c);
if(ans<a)
cout << "F";
else if(ans<a+b)
cout<<"M";
else
cout << "T";
return 0;
}
B - Hit and Blow
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int a[N], b[N];
map<int, int> mp;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,cnt=0,cnt1=0;
cin >> n;
for (int i = 1; i <= n;i++)
{
cin >> a[i];
mp[a[i]]=1;
}
for (int i = 1; i <= n;i++)
{
cin >> b[i];
if(mp[b[i]]==1&&a[i]!=b[i])
cnt1++;
if(a[i]==b[i])
cnt++;
}
cout << cnt << endl<< cnt1;
return 0;
}
C - Collision 2
思路是对的但是实现搞复杂了,总之就是y轴一样的排在一起,然后找一下相邻的位置有无相反的
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e8;
struct node
{
int x,y;
char z;
}a[N];
bool cmp(node a, node b)
{
if(a.y==b.y)
return a.x < b.x;
return a.y < b.y;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
for (int i = 1; i <= n;i++)
{
cin >> a[i].x >> a[i].y;
}
string s;
cin >> s;
for (int i = 1; i <= n;i++)
{
a[i].z = s[i - 1];
}
sort(a + 1, a + 1 + n,cmp);
for(int i=2;i<=n;i++)
{
if(a[i].y==a[i-1].y&&a[i].z=='L'&&a[i-1].z=='R')
{
cout<<"Yes";
return 0;
}
}
cout<<"No";
return 0;
}