A Primary Task
签到题
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int qmi(int a, int b, int p)
{
int res = 1;
while (b)
{
if (b & 1)res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
void solve()
{
string s;
cin >> s;
if( s[0] == '1' && s[1] == '0' ){
if( s[2] > '0' )
{
int num = stoi( s.substr(2 , s.size() - 2 ));
if(num >= 2 ){
cout << "YES"<< endl;return;
}
}
}
cout << "NO" << endl;
}
signed main()
{
int tt; cin >> tt;
while (tt--)solve();
return 0;
}
B Seating in a Bus
思路:模拟
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
bool st[N];
int qmi(int a, int b, int p)
{
int res = 1;
while (b)
{
if (b & 1)res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
void solve()
{
memset( st , 0 , sizeof st );
int n;cin >> n;
vector<int> a (n + 1);
for(int i= 1;i <=n; i++)cin >> a[i];
st[a[1]] = true;
for( int i = 2; i <= n; i++ )
{
if( st[a[i] + 1] || st[a[i] - 1] ){
st[a[i]] = true;
}
else {
cout << "NO" << endl;return;
}
}
cout << "YES" << endl;
}
signed main()
{
int tt; cin >> tt;
while (tt--)solve();
return 0;
}
C Numeric String Template
题意:
思路:模拟
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int a[N]; int n;
int qmi(int a, int b, int p)
{
int res = 1;
while (b)
{
if (b & 1)res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
bool check1(string& s)
{
map<int, vector<int>> mp;
for (int i = 1; i <= n; i++)
{
mp[a[i]].push_back(i);
}
for (auto e : mp)
{
char ch = s[e.second[0]];
if (e.second.size() >= 2)
{
auto& temp = e.second;
for (int i = 1; i < temp.size(); i++)
{
if (s[temp[i]] != ch)return false;
}
}
}
return true;
}
bool check2(string& s)
{
map<char, vector<int>> mp;
for (int i = 1; i < s.size(); i++)
{
mp[s[i]].push_back(i);
}
for (auto e : mp)
{
auto& temp = e.second;
int flag = a[temp[0]];
if (temp.size() >= 2)
{
for (int i = 1; i < temp.size(); i++)
{
if (a[temp[i]] != flag)return false;
}
}
}
return true;
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int m;
cin >> m;
while (m--)
{
string s; cin >> s;
if (s.size() != n) {
cout << "NO" << endl; continue;
}
s = " " + s;
if (!check1(s))
{
cout << "NO" << endl; continue;
}
if (!check2(s)) {
cout << "NO" << endl; continue;
}
cout << "YES" << endl;
}
}
signed main()
{
int tt; cin >> tt;
while (tt--)solve();
return 0;
}
D Right Left Wrong
题意:
思路:贪心 + 双指针
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int pre[N]; int a[N];
int qmi(int a, int b, int p)
{
int res = 1;
while (b)
{
if (b & 1)res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
void solve()
{
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];
string s; cin >> s; int size = s.size();
int left = 1;
int right = size;
int sum = 0; s = " " + s;
while (right > left)
{
while (right > left && s[left] != 'L')left++;
while (right > left && s[right] != 'R')right--;
if (right > left)
{
sum += pre[right] - pre[left - 1];
left++; right--;
}
}
cout << sum << endl;
}
signed main()
{
int tt; cin >> tt;
while (tt--)solve();
return 0;
}
E Photoshoot for Gorillas
题意:
思路:
计算每个格子的贡献 -> 二维数组差分
补充:母题(二维数组差分)
题意:
核心操作代码:
void insert(int x1,int y1,int x2,int y2,int c) // 核心操作代码
{
b[x1][y1]+=c;
b[x2+1][y1]-=c;
b[x1][y2+1]-=c;
b[x2+1][y2+1]+=c;
}
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int a[N];
int n, m;
void insert(vector<vector<int>>& b, int x1, int y1, int x2, int y2)
{
b[x1][y1]++; b[x2 + 1][y1]--; b[x1][y2 + 1]--; b[x2 + 1][y2 +1 ]++;
}
void solve()
{
int k, w; cin >> n >> m >> k >> w;
for (int i = 1; i <= w; i++) cin >> a[i];
sort(a + 1, a + w + 1, greater<int>());
vector<vector<int>> b(n + 100, vector<int>(m + 100));
for (int i = 1; i + k - 1 <= n; i++)
{
for (int j = 1; j + k - 1 <= m; j++)
{
insert(b, i, j, i + k - 1, j + k - 1);
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
}
}
vector<int> c;
c.push_back(0x3f3f3f3f);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)c.push_back(b[i][j]);
sort(c.begin(), c.end(), greater<int>());
int ans = 0;
for (int i = 1; i <= w; i++)
ans += a[i] * c[i];
cout << ans << endl;
}
signed main()
{
int tt; cin >> tt;
while (tt--)solve();
return 0;
}
F Color Rows and Columns
题意:
思路:dp
1. 状态表示:前 i 个矩阵中获得 至少 j 分的最小操作次数
2. 状态转移方程:f[i][j] = min( { f[i][j] , f[i-1][j] , f[i-1][ j - score] + cnt } );
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
const int N = 1e3 + 10;
const int M = 110;
int f[N][M];
const int INF = 0x3f3f3f3f3f3f3f3f;
void solve()
{
int n , k;cin >> n >> k;
memset( f , 0x3f , sizeof f );
f[0][0] = 0;
for( int i = 1; i <= n;i++)
{
int a, b;cin >> a >> b;
int cnt = 0 , score = 0;
while( a && b )
{
if( a > b )swap(a , b );
if( a == 1 && b == 1 )
{
cnt++;
score += 2;
a = 0; b = 0;
}
else{
cnt += a;
score++;
b--;
}
for( int j = 0 ; j <= k ;j++)
{
f[i][j] = min( f[i][j] , f[i-1][j] );
f[i][j] = min( f[i][j] , f[i-1][max(0ll , j - score)] + cnt );
}
}
}
if( f[n][k] == INF )cout << -1 << endl;
else cout << f[n][k] << endl;
}
signed main()
{
int tt;cin >> tt;
while(tt--)solve();
return 0;
}