牛客小白月赛83(A-E)
牛客小白月赛83(A-E)
A 小天的金银铜铁
代码
点击查看代码
// Problem: 小天的金银铜铁
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/72041/A
// Memory Limit: 262144 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define sc second
#define endl '\n'
#define pb push_back
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
#define debug(x) cout<<"----Line#"<<x<<"----"<<endl
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
typedef pair<char, char> PCC;
typedef pair<int, string> PIS;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const ll mod = 1e9 + 7;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
const int N = 1010;
void solve () {
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
int n, m, x, y;
cin >> n >> m >> x >> y;
if (n * a + m * b + c * x + d * y >= e) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return ;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
//cin >> T;
while (T --) {
solve ();
}
return 0;
}
B 小天的魔法
分析
sort排序之后贪心即可
点击查看代码
// Problem: 小天的魔法
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/72041/B
// Memory Limit: 262144 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define sc second
#define endl '\n'
#define pb push_back
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
#define debug(x) cout<<"----Line#"<<x<<"----"<<endl
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
typedef pair<char, char> PCC;
typedef pair<int, string> PIS;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const ll mod = 1e9 + 7;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
const int N = 1010;
int n, m, x;
int a[N], b[N];
void solve () {
cin >> n >> m >> x;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++) cin >> b[i];
sort(a + 1, a + n + 1);
sort(b + 1, b + n + 1);
ll s = 0;
int cn = 0;
for (int i = n; i >= 1; i --) {
if (s + b[i] >= x) {
cn ++;
cout << cn << endl;
return ;
}
s += a[i] * b[i];
if (s >= x) {
cn += 2;
cout << cn << endl;
return ;
}
cn += 2;
}
cout << -1 << endl;
return ;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
//cin >> T;
while (T --) {
solve ();
}
return 0;
}
C 小天的 Minecraft
分析
一共只有三种达成目标的情况,分别是16铜,12铜4银,12铜4金
点击查看代码
// Problem: 小天的 Minecraft
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/72041/C
// Memory Limit: 262144 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define sc second
#define endl '\n'
#define pb push_back
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
#define debug(x) cout<<"----Line#"<<x<<"----"<<endl
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
typedef pair<char, char> PCC;
typedef pair<int, string> PIS;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const ll mod = 1e9 + 7;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
const int N = 1010;
void solve () {
int a, b, c; cin >> a >> b >> c;
double ans = 1820 * pow(a * 1.0 / 16, 12) * (pow(b * 1.0 / 16, 4) + pow(c * 1.0 / 16, 4)) + pow(a * 1.0 / 16, 16);
printf("%.10lf\n", ans);
return ;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
cin >> T;
while (T --) {
solve ();
}
return 0;
}
D 小天的子序列
分析
如果使用暴力求解,我们可以通过枚举区间加组合数的方式n^2求出题目要求的子序列数量。但是会TLE
使用mp数组,mp[x][y][l]意义为以x开头,y结尾,长度为l的子串的数量,可以通过初始化mp数组来求出答案。在每个询问时,我们已经知道了以x开头y结尾,长度为len~n的所有子串的数量,因此可以O(n)求出答案
点击查看代码
// Problem: 小天的子序列
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/72041/D
// Memory Limit: 262144 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define sc second
#define endl '\n'
#define pb push_back
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
#define debug(x) cout<<"----Line#"<<x<<"----"<<endl
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
typedef pair<char, char> PCC;
typedef pair<pair<char, char>, int> PCCI;
typedef pair<int, string> PIS;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const ll mod = 998244353;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
const int N = 510;
int n, m, c[N][N];
int mp[26][26][N];
void init() {
for(int i = 0; i < N; i ++) {
for(int j = 0; j <= i; j ++) {
if (!j)
c[i][j] = 1;
else {
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
}
}
}
void solve () {
cin >> n;
string s; cin >> s;
for (int i = 0; i < n; i ++) {
for (int j = i + 1; j < n; j ++) {
mp[s[i] - 'a'][s[j] - 'a'][j - i + 1] ++;
}
}
cin >> m;
while (m --) {
char x, y; cin >> x >> y;
int len; cin >> len;
ll ans = 0;
for (int i = len; i <= n; i ++) {
ans = (ans + mp[x - 'a'][y - 'a'][i] * c[i - 2][len - 2]) % mod;
}
cout << ans << endl;
}
return ;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
init();
//cin >> T;
while (T --) {
solve ();
}
return 0;
}
E 小天的贪吃蛇
分析
可以将格子转化为序列
我们考虑使用set维护每个字符的所有下标,因此对于每一个2/3类型的询问{x,y},首先将其转化为一维下标pos,然后可以对除了此字符以外的所有字符的set进行二分,求出pos之后第一个出现的位置的最小值,就是最终的答案
点击查看代码
// Problem: 小天的贪吃蛇
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/72041/E
// Memory Limit: 262144 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define sc second
#define endl '\n'
#define pb push_back
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
#define debug(x) cout<<"----Line#"<<x<<"----"<<endl
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
typedef pair<char, char> PCC;
typedef pair<int, string> PIS;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const ll mod = 1e9 + 7;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
const int N = 1e5 + 10;
int n, m, q;
string col, row;
map<PII, int> mp1, mp2;
set<int> s1[26], s2[26];
void solve () {
cin >> n >> m;
vector s(n + 1, vector<char> (m + 1));
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++) {
cin >> s[i][j];
}
}
for (int i = 1, v = 1; i <= n; i ++)
if (i & 1)
for (int j = 1; j <= m; j ++)
mp1[{i, j}] = v ++;
else
for (int j = m; j >= 1; j --)
mp1[{i, j}] = v ++;
for (int j = 1, v = 1; j <= m; j ++)
if (j & 1)
for (int i = 1; i <= n; i ++)
mp2[{i, j}] = v ++;
else
for (int i = n; i >= 1; i --)
mp2[{i, j}] = v ++;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= m; j ++) {
int ch = s[i][j] - 'a';
s1[ch].insert(mp1[{i, j}]);
s2[ch].insert(mp2[{i, j}]);
}
cin >> q;
while (q --) {
int op, x, y; cin >> op >> x >> y;
if (op == 1) {
char c; cin >> c;
char ch = s[x][y];
s1[ch - 'a'].erase(mp1[{x, y}]);
s2[ch - 'a'].erase(mp2[{x, y}]);
s1[c - 'a'].insert(mp1[{x, y}]);
s2[c - 'a'].insert(mp2[{x, y}]);
s[x][y] = c;
} else if (op == 2) {
char ch = s[x][y];
int ans = INF;
for (char c = 'a'; c <= 'z'; c ++) {
if (c == ch) continue;
else {
auto pos = s1[c - 'a'].lower_bound(mp1[{x, y}]);
if (pos == s1[c - 'a'].end()) {
ans = min(ans, n * m - mp1[{x, y}] + 1);
} else {
ans = min(ans, *pos - mp1[{x, y}]);
}
}
}
cout << ans << endl;
} else {
char ch = s[x][y];
int ans = INF;
for (char c = 'a'; c <= 'z'; c ++) {
if (c == ch) continue;
else {
auto pos = s2[c - 'a'].lower_bound(mp2[{x, y}]);
if (pos == s2[c - 'a'].end()) {
ans = min(ans, n * m - mp2[{x, y}] + 1);
} else {
ans = min(ans, *pos - mp2[{x, y}]);
}
}
}
cout << ans << endl;
}
}
return ;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
//cin >> T;
while (T --) {
solve ();
}
return 0;
}
F 小天的 A+B
明天再补

浙公网安备 33010602011771号