好久没写题解了,今天来写个题解。
A - 问题 Generator
#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 pi pair<int,int>
#define vi vector<int>
#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;
cin>>n>>m;
string s;
cin>>s;
map<char,int> mp;
//int n=s.size();
for (int i=0;i<n;i++){
mp[s[i]]++;
}
int sum=0;
for (char i='A';i<='G';i++){
if(mp[i]<m){
sum+=(m-mp[i]);
}
}
cout<<sum<<endl;
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
B - Choosing Cubes
#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 pi pair<int,int>
#define vi vector<int>
#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,f,k;
cin>>n>>f>>k;
int x;
vi a(n+1);
for (int i=1;i<=n;i++){
cin>>a[i];
if(i==f){
x=a[i];
}
}
int pos=0;
sort(a.begin()+1,a.end());
reverse(a.begin()+1,a.end());
for (int i=1;i<=n;i++){
if(a[i]==x){
pos=i;
break;
}
}
int cnt=pos;
for (int i=pos+1;i<=n;i++){
if(a[i]==a[pos]){
cnt++;
}
else {
break;
}
}
if(cnt<=k){
cout<<"YES"<<endl;
}
else if (cnt>k && pos<=k){
cout<<"MAYBE"<<endl;
}
else if(cnt>k){
cout<<"NO"<<endl;
}
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
A和B 都是简单的模拟题,按照题意来写就行,可以参考代码。
C - Sofia and the Lost Operations
思路:给出的m个元素可以分为三类来看,一类是需要改成b的,一类是和b相等的元素,还有一类是ab 中都没有的元素。 而这第三类元素必须要被第一类和第二类覆盖掉。所以只需要倒叙找最后的元素是不是第一第二类元素。 (可以使用map 来存第一第二类元素)。
#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 pi pair<int,int>
#define vi vector<int>
#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),b(n+1);
for (int i=1;i<=n;i++){
cin>>a[i];
}
for (int i=1;i<=n;i++){
cin>>b[i];
}
map<int,int> mp,mp1;
for (int i=1;i<=n;i++){
if(a[i]!=b[i]){
mp[b[i]]++;
}
else {
mp1[b[i]]=1;
}
}
int m;
cin>>m;
int fl=0;
vi c;
for (int i=1;i<=m;i++){
int x;
cin>>x;
c.push_back(x);
}
int pos;
for (int i=m-1;i>=0;i--){
if(mp[c[i]]|| mp1[c[i]]){
pos=i;
break;
}
}
for (int i=0;i<m;i++){
if(mp[c[i]]){
mp[c[i]]--;
}
else if(mp1[c[i]]){
continue;
}
else {
if(i>pos){
fl=1;
}
}
}
if(fl==1){
cout<<"NO"<<endl;
return ;
}
for (int i=1;i<=n;i++){
if(mp[b[i]]){
cout<<"NO"<<endl;
return ;
}
}
cout<<"YES"<<endl;
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
D - GCD-sequence
思路:通过贪心来遍历没所以最多只能处理一个递减的情况,可以先开个数组记录一下递减的位置和递减的对数数量。一点要特判的是在边界的话,是可以直接删掉最外面的数的。然后可以直接遍历一次,每次都对删去中间那个a。看操作后,是不是可以消去所有的不递增。
可以看代码理解。
#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 pi pair<int,int>
#define vi vector<int>
#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];
}
int ans=0;
vi b(n),c(n+1);
for (int i=1;i<=n-1;i++){
b[i]=__gcd(a[i],a[i+1]);
}
for (int i=1;i<n-1;i++){
if(b[i+1]<b[i]){
c[i]=1;
ans++;
}
}
if(ans==0){
cout<<"YES"<<endl;
return ;
}
if(ans==1 &&c[1]==1){
cout<<"YES"<<endl;
return ;
}
if(ans==1 &&c[n-2]==1){
cout<<"YES"<<endl;
return ;
}
int fl=0;
for (int i=1;i<n-1;i++){
int tmp1=0,tmp2=0,tmp3=1e9;
tmp2=__gcd(a[i],a[i+2]);
int cnt=ans;
if(c[i]) cnt--;
if(c[i-1]) cnt--;
if(c[i+1] ) cnt--;
if(i>1){
tmp1=b[i-1];
}
if(i<n-2){
tmp3=b[i+2];
}
if(tmp1>tmp2){
cnt++;
}
if(tmp2>tmp3){
cnt++;
}
if(cnt==0){
fl=1;
}
}
if(fl){
cout<<"yes"<<endl;
return ;
}
else {
cout<<"NO"<<endl;
}
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
E - Permutation of Rows and Columns
思路:其实就是看两个矩阵的每行和每列的元素是不是一样的。
所以用两个map 分别存每个元素的x和 y坐标 然后最后看两个矩阵的每一个元素的x和坐标是不是对应的。
#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 pi pair<int,int>
#define vi vector<int>
#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;
map<int,int> mpx,mpy;
void solve()
{
int n,m;
cin>>n>>m;
vector<vector<int>> a(n+1,vector<int>(m+1));
vector<vector<int>> b(n+1,vector<int>(m+1));
int fl=0;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin>>a[i][j];
mpx[a[i][j]]=i;
mpy[a[i][j]]=j;
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin>>b[i][j];
if(mpx[b[i][j]]!=mpx[b[i][1]] || mpy[b[i][j]]!=mpy[b[1][j]]){
fl=1;
}
}
}
if(fl){
cout<<"NO"<<endl;
return ;
}
else {
cout<<"yes"<<endl;
}
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
Field Division (easy version)
思路:从底下往上算,每次都修改最左边的值和最下面的值,并加上左边的面积,就是总面积了。
其中x轴的排序时从大到小,y轴的排序是从小到大。 如果想移除这个台灯后面积变大,这个台灯必须得位于边界,并且两个相邻的边为边界才行, 这个点也是边界改变的点。 所以每次改变边界的时候都把这个点标记。
#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 pi pair<int,int>
#define vi vector<int>
#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;
struct node{
int x,y;
int id;
}a[200010];
bool cmp(node x,node y){
if(x.x==y.x){
return x.y<y.y;
}
else {
return x.x>y.x;
}
}
void solve()
{
int n,m,k;
cin>>n>>m>>k;
for (int i=1;i<=k;i++){
cin>>a[i].x>>a[i].y;
a[i].id=i;
}
sort(a+1,a+k+1,cmp);
vi ans(k+1);
int l=m+1,d=n;
int sum=0;
for (int i=1;i<=k;i++){
if(l>a[i].y){
ans[a[i].id]=1;
sum+=(d-a[i].x)*(l-1);
l=a[i].y;
d=a[i].x;
}
}
sum+=d*(l-1);
cout<<sum<<endl;
for (int i=1;i<=k;i++){
cout<<ans[i]<<" ";
}
cout<<endl;
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}