A. Primary Task
简单的模拟题,按照题意模拟即可
cpp
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define all2(x) x.begin()+1,x.end()
#define pi pair<int,int>
#define vi vector<int>
#define vc vector<char>
#define si set<int>
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;
#define pb(x) push_back(x)
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;
void solve()
{
string s;
cin>>s;
int n=s.size();
int sum=0;
if(s[0]!='1' || s[1]!='0'){
cout<<"NO"<<endl;
return ;
}
for (int i=2;i<n;i++){
sum=sum*10+(s[i]-'0');
if(sum==0){
cout<<"No"<<endl;
return ;
}
}
if(sum<2){
cout<<"NO"<<endl;
}
else {
cout<<"Yes"<<endl;
}
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
B. Seating in a Bus
按照顺序记录,只要旁边无人的时候入座的话,就是违规。
cpp
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define all2(x) x.begin()+1,x.end()
#define pi pair<int,int>
#define vi vector<int>
#define vc vector<char>
#define si set<int>
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;
#define pb(x) push_back(x)
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;
void solve()
{
int n;
cin>>n;
vi a(n+1);
for (int i=1;i<=n;i++){
cin>>a[i];
}
vi b(n+1,0);
int fl=0;
for (int i=1;i<=n;i++){
int x=a[i];
if(fl==0){
fl=1;
}
else if(x==1){
if(b[x+1]==0){
cout<<"NO"<<endl;
return ;
}
}
else if(x==n){
if(b[x-1]==0){
cout<<"NO"<<endl;
return ;
}
}
else {
if(b[x-1]==0 && b[x+1]==0){
cout<<"NO"<<endl;
return ;
}
}
b[x]=1;
}
cout<<"YES"<<endl;
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
C. Numeric String Template
开两个map分别记录字符和数字,如果发现出现无法对应,或者是前后矛盾的情况就是错误。
cpp
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define all2(x) x.begin()+1,x.end()
#define pi pair<int,int>
#define vi vector<int>
#define vc vector<char>
#define si set<int>
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;
#define pb(x) push_back(x)
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;
void solve()
{
int n;
cin>>n;
vi a(n);
for (int i=0;i<n;i++){
cin>>a[i];
}
int m;
cin>>m;
for (int i=1;i<=m;i++){
string s;
cin>>s;
map<char,int> mp;
map<int,int> mp2;
if((int)s.size()!=n){
cout<<"NO"<<endl;
}
else {
int fl=0;
for (int j=0;j<n;j++)
{
if(mp[s[j]]==0 && mp[a[j]]==0){
mp[s[j]]=a[j];
mp2[a[j]]=s[j]-'a'+1;
}
else if(mp[s[j]] && mp2[a[j]]){
char tmp=mp2[a[j]]-1+'a';
if(s[j]!=tmp){
cout<<"NO"<<endl;
fl=1;
break;
}
}
else {
cout<<"NO"<<endl;
fl=1;
break;
}
}
if(fl==0){
cout<<"Yes"<<endl;
}
}
}
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
D. Right Left Wrong
双指针,并使用前缀和优化,发现左边是L,右边是R时,就加上中间的部分,否则,当左边不是L,和右边不是R的情况,都分别前进一步。
cpp
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define all2(x) x.begin()+1,x.end()
#define pi pair<int,int>
#define vi vector<int>
#define vc vector<char>
#define si set<int>
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;
#define pb(x) push_back(x)
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;
void solve()
{
int n;
cin>>n;
vi a(n+1),s(n+1);
for (int i=1;i<=n;i++){
cin>>a[i];
}
for (int i=1;i<=n;i++){
s[i]=s[i-1]+a[i];
}
int sum=0;
string s1;
cin>>s1;
s1=" "+s1;
int l=1,r=n;
while(l<r){
if(s1[l]=='L' && s1[r]=='R'){
sum+=(s[r]-s[l-1]);
l++,r--;
}
else if(s1[l]!='L'){
l++;
}
else if(s1[r]!='R'){
r--;
}
}
cout<<sum<<endl;
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
E. Photoshoot for Gorillas
本题n*m小于等于2e5,可以暴力通过,可以先暴力枚举算出每个地块受到多少个小方格的影响,
让最大的高度站在受影响次数最多方格位置。
cpp
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define all2(x) x.begin()+1,x.end()
#define pi pair<int,int>
#define vi vector<int>
#define vc vector<char>
#define si set<int>
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;
#define pb(x) push_back(x)
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;
void solve()
{
int n,m,k;
cin>>n>>m>>k;
int w;
cin>>w;
vi a(w);
for (int i=0;i<w;i++){
cin>>a[i];
}
sort(a.begin(),a.end());
reverse(all(a));
int sum=0;
vector<vector<int>> v(n+1,vector<int>(m+1));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
int k1 = min(n - k, i) - max((int)0, i - k + 1) + 1;
int k2 = min(m - k, j) - max((int)0, j - k + 1) + 1;
v[i][j]=k1*k2;
}
}
vi g;
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
g.push_back(v[i][j]);
}
}
sort(all(g));
reverse(all(g));
for (int i=0;i<w;i++){
sum+=g[i]*a[i];
}
cout<<sum<<endl;
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
F. Color Rows and Columns
背包变形 .dp[i] 可表示 第i分需要最少的方格次数。
发现当a不等于b的时候,我们会优先选择a这条边,在填到相等后遍会选择一次各填一边,可以节省一个方格。所以在此的基础上分为两类进行背包的转移即可。
cpp
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define all2(x) x.begin()+1,x.end()
#define pi pair<int,int>
#define vi vector<int>
#define vc vector<char>
#define si set<int>
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;
#define pb(x) push_back(x)
#define fi first
#define sc second
#define is insert
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int INF =1e18;
signed main()
{
IOS
//.........................//
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
vi dp(k+1,0x3f3f3f3f);
dp[0]=0;
for (int i=1;i<=n;i++){
int a,b;
cin>>a>>b;
if(a>b){
swap(a,b);
}
for(int j=k;j>=0;j--){
for (int l=0;l<=a+b && l+j<=k ;l++){
if(l<=b-a){
dp[l+j]=min(dp[j+l],dp[j]+a*l);
}
else {
int x,y;
x=(a+b-l)/2;
y=a+b-l-x;
int ans=a*b-x*y;
dp[j+l]=min(dp[j+l],dp[j]+ans);
}
}
}
}
if(dp[k]==0x3f3f3f3f){
cout<<-1<<endl;
}
else {
cout<<dp[k]<<endl;
}
}
}