2022.4.22
阿新 • • 發佈:2022-04-22
Codeforces Round #784 (Div. 4)
A. Division?
#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=1e9; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin>>t; while(t--) { int n; cin >> n; if(n>=1900) { cout << "Division 1"; } else if(n>=1600&&n<=1899) { cout << "Division 2"; } else if(n>=1400&&n<=1599) { cout << "Division 3"; } else if(n<=1399) cout << "Division 4"; cout<< "\n"; } return 0; }
B - Triple
#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=1e9; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin>>t; while(t--) { map<int, int> mp; int n; cin >> n; int f = 0; for (int i = 1; i <= n;i++) { int x; cin >> x; mp[x]++; if(mp[x]>=3&&!f) { f = 1; cout << x << '\n'; } } if(!f) cout << "-1\n"; } return 0; }
C - Odd/Even Increments
#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=50+10,INF=1e9; int a[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin>>t; while(t--) { int n; cin >> n; int f = 0; int f1 = 0, f2 = 0; for (int i = 1; i <= n;i++) { cin >> a[i]; } for (int i = 1; i <= n; i += 2) { f1 = a[1]; if(f1&1) { if(!(a[i]&1)) f=1; } else { if((a[i]&1)) f=1; } } if(n>=2) { for (int i = 2; i <= n; i += 2) { f2 = a[2]; if(f2&1) { if(!(a[i]&1)) f=1; } else { if((a[i]&1)) f=1; } } } if(f) cout << "no\n"; else cout << "yes\n"; } return 0; }
D - Colorful Stamp
觀察發現,2個w之間R和B都要同時出現
#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=1e9;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
int n;
cin >> n;
string s;
cin >> s;
s += 'W';
int cnt = 0, f = 0, fr = 0, fb = 0, ff = 0;
for (int i = 0; i <n+1;i++)
{
if(s[i]!='W')
{
if(s[i]=='R')
fr = 1;
else if(s[i]=='B')
fb = 1;
}
else if(s[i]=='W')
{
if(i>0&&s[i-1]!='W')
{
if(fr==0||fb==0)
{
f = 1;
break;
}
fr = 0, fb = 0;
}
}
}
for (int i = 0; i < n;i++)
{
if(s[i]!='W')
{
ff = 1;
}
}
if(!ff)
{
cout << "yes\n";
}
else
{
if(!f)
cout << "yes\n";
else cout<<"no\n";
}
}
return 0;
}
E - 2-Letter Strings
每次讀入的時候都加上就不會超時了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<char,char> pcc;
const int N=1e5+10,INF=1e9;
int cnt[30][30];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
int n;
memset(cnt, 0, sizeof cnt);
cin >> n;
ll ans = 0;
for (int i = 1; i <= n;i++)
{
char c, d;
cin >> c >> d;
int a = c - 'a', b = d - 'a';
for (int j = 0; j < 26;j++)
{
for (int k = 0; k < 26;k++)
{
if((a==j&&b!=k)||(a!=j&&b==k))
ans += cnt[j][k];
}
}
cnt[a][b]++;
}
cout << ans << '\n';
}
return 0;
}
G. Fall Down
自己實現的時候有點問題,看到別人用ans標記最後輸出,就嘗試了一下,不過寫的有點長。
#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=50+10,INF=1e9;
char g[N][N];
int ans[N][N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--)
{
int n,m;
memset(ans, 0, sizeof ans);
cin >> n>>m;
for (int i = 1; i <= n;i++)
{
for (int j = 1; j <= m;j++)
{
cin >> g[i][j];
}
}
for (int i = 1; i <= n;i++)
{
for (int j = 1;j<= m;j++)
{
if(g[i][j]=='o')
{
int cnt = 0;
ans[i][j] = 2;
for (int k = i; k>=1;k--)
{
if(k==i)
continue;
if(g[k][j]=='o')
break;
if(g[k][j]=='*')
{
cnt++;
}
}
for (int k = i-1; cnt;k--,cnt--)
{
ans[k][j] = 1;
}
}
}
}
for (int j = 1;j<= m;j++)
{
if(g[n][j]!='o')
{
int cnt = 0, f = 0;
if(g[n][j]=='*')
ans[n][j] = 1;
else
f = 1;
for (int k = n; k>=1;k--)
{
if(k==n)
continue;
if(g[k][j]=='o')
break;
if(g[k][j]=='*')
{
cnt++;
}
}
if(cnt)
ans[n][j] = 1;
for (int k = n-1+f; cnt;k--,cnt--)
{
ans[k][j] = 1;
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if(ans[i][j]==2)
cout << 'o';
else if(ans[i][j]==1)
cout << '*';
else
cout << '.';
}
cout << '\n';
}
cout << '\n';
}
return 0;
}
H - Maximal AND
統計一下每個數的哪一位是1,如果想要與運算後最大的話,有一的位必須所有數都是1.於是考慮貪心從高到低列舉每一位,如果還有次數就將這n個數的這一位都變成1,同時減去相應的次數,最後把和加上即可。
#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=1e9;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--)
{
int cnt[35]={0};
int n, k;
cin >> n >> k;
for (int i = 1; i <= n;i++)
{
int x;
cin >> x;
for (int j = 0; j <= 30;j++)
{
if(x>>j&1)
cnt[j]++;
}
}
ll ans = 0;
for (int i = 30; i >= 0;i--)
{
if(k>=n-cnt[i])
{
k -= n - cnt[i];
ans += 1<<i;
}
}
cout << ans << '\n';
}
return 0;