A. CF1144A Diverse Strings
B. CF1144B Parity Alternated Deletions
C. CF1144C Two Shuffled Sequences
D. CF1144D Equalize Them All
E. CF1144F Graph Without Long Directed Paths
F. CF448C Painting Fence
G. CF1499D The Number of Pairs
H. CF1398D Colored Rectangles
A. CF1144A Diverse Strings
我们可以先排序再判断有无重复和有没有连在一起
#include
using namespace std;
string a;
int b[30];
int main()
{
int i,j,k,n,l,r;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a;
j=a.length();
if(j>26)
{
cout<<"No"<<endl;
continue;
}
for(k=0;k<=29;k++)
{
b[k]=0;
}
l=27;
r=-1;
for(k=0;k<j;k++)
{
if(b[a[k]-'a']!=0)
{
l=27;
r=-1;
cout<<"No"<<endl;
break;
}
b[a[k]-'a']++;
l=min(l,a[k]-'a');
r=max(r,a[k]-'a');
}
for(k=l;k<=r;k++)
{
if(b[k]!=1)
{
l=27;
r=-1;
cout<<"No"<<endl;
break;
}
}
if(l!=27&&r!=-1)
cout<<"Yes"<<endl;
}
}
B. CF1144B Parity Alternated Deletions
首先我们肯定是看奇数和偶数的个数哪个多,优先从多的开始,如果只相差1个那也是能删除完的,再把最小的留下来。
#include
#include
using namespace std;
//string a;
int a[2004],b[2004];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int i,j,k,n,l,r,an;
cin>>n;
l=0;
r=0;
an=0;
for(i=1;i<=n;i++)
{
cin>>j;
if(j%2==0)
{
l++;
a[l]=j;
}
else
{
r++;
b[r]=j;
}
}
sort(b+1,b+r+1,cmp);
sort(a+1,a+l+1,cmp);
if(a==b||a==b+1||b==a+1)
{
cout<<0;
return 0;
}
for(i=r+2;i<=l;i++)
{
an+=a[i];
}
for(i=l+2;i<=r;i++)
{
an+=b[i];
}
cout<<an;
}
CF1144C Two Shuffled Sequences
首先肯定是先排个序,再看看能不能正着弄一个单调的,倒着弄一个单调的
#include
#include
using namespace std;
//string a;
int a[200009],b[200009],c[200009];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int i,j,k,n,l,r,an;
b[0]=-1;
c[0]=-1;
cin>>n;
l=0;r=0;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
for(i=1;i<=n;i++)
{
if(b[l]<a[i])
{
l++;
b[l]=a[i];
}
else
{
r++;
c[r]=a[i];
}
}
for(i=2;i<=r;i++)
{
if(c[i]<=c[i-1])
{
cout<<"NO";
return 0;
}
}
cout<<"YES"<<endl;
if(l==0)
{
cout<<0<<endl;
}
else
{
cout<<l<<endl;
for(i=1;i<=l;i++)
{
cout<<b[i]<<" ";
}
}
cout<<endl;
if(r==0)
{
cout<<0<<endl;
}
else
{
cout<<r<<endl;
for(i=r;i>=1;i--)
{
cout<<c[i]<<" ";
}
}
}
D. CF1144D Equalize Them All
嘿嘿嘿,那就找出现次数最多的,把他们变成他就好啦
```cpp
#include
#include
using namespace std;
//string a;
int a[200009],b[200009],c[200009];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int i,j,k,n,l,r,an;
cin>>n;
l=0;
r=0;
k=0;
for(i=1;i<=n;i++)
{
cin>>a[i];
b[a[i]]++;
if(b[a[i]]>l)
{
l=b[a[i]];
r=a[i];
k=i;
}
}
cout<<n-l;
if(n-l==0)
{
return 0;
}
cout<<endl;
for(i=k+1;i<=n;i++)
{
if(a[i]>r)
{
cout<<2<<' '<<i<<' '<<i-1<<endl;
}
if(a[i]<r)
{
cout<<1<<' '<<i<<' '<<i-1<<endl;
}
}
for(i=k-1;i>=1;i--)
{
if(a[i]>r)
{
cout<<2<<' '<<i<<' '<<i+1<<endl;
}
if(a[i]<r)
{
cout<<1<<' '<<i<<' '<<i+1<<endl;
}
}
}
E. CF1144F Graph Without Long Directed Paths
这是一道简单的黑白染色题啦
#include
#include
#include
using namespace std;
//string a;
queue<int> q;
int h[400009],ne[400009],to[400009],num,w[400009],f[400009][2],t[400009];
bool cmp(int x,int y)
{
return x>y;
}
void add(int a,int b)
{
num++;
ne[num]=h[a];
h[a]=num;
to[num]=b;
}
bool bfs()
{
int i,j,k,v;
while(!q.empty())
{
v=q.front();
q.pop();
for(i=h[v];i;i=ne[i])
{
j=to[i];
if(t[j]==0||t[j]==-t[v])
{
t[j]=-t[v];
}
else
{
return false;
}
if(w[j]==0)
{
w[j]=1;
q.push(j);
}
}
}
return true;
}
int main()
{
int i,j,k,n,l,r,an,m;
cin>>n>>m;
for(i=1;i<=m;i++)
{
cin>>j>>k;
f[i][0]=j;
f[i][1]=k;
add(j,k);
add(k,j);
}
w[1]=1;
t[1]=1;
q.push(1);
if(!bfs())
{
cout<<"NO";
return 0;
}
cout<<"YES"<<endl;
for(i=1;i<=m;i++)
{
if(t[f[i][0]]>t[f[i][1]])
{
cout<<1;
}
else
{
cout<<0;
}
}
}
F. CF448C Painting Fence
有一点点贪心和分治的感觉,就是每次找他们最小的横着刷,再和竖着刷比较一下哪个刷的少就可以啦
#include
using namespace std;
long long n,a[50004],an;
long long dg(long long l,long long r,long long o)
{
if(l>r)
{
return 0;
}
long long j,i,k;
j=1e9+10;
k=l-1;
for(i=l;i<=r;i++)
{
if(j>a[i])
{
j=a[i];
k=i;
}
}
return min(dg(l,k-1,a[k])+dg(k+1,r,a[k])+a[k]-o,r-l+1);
}
int main()
{
long long i,j,k;
cin>>n;
j=1e9+10;
k=0;
for(i=1;i<=n;i++)
{
cin>>a[i];
if(j>a[i])
{
j=a[i];
k=i;
}
}
an=min(dg(1,k-1,a[k])+dg(k+1,n,a[k])+a[k],n);
cout<<an;
}
G. CF1499D The Number of Pairs
有一个需要注意的点就是数组要开到2E7+7,因为k1,k2,的乘积是小于等于2e7的,还有要用线性筛求质因子个数,要不然会超时的
#include
#include
using namespace std;
long long a[20000007],b[20000007],num,an,e[20000007];
int main()
{
long long i,j,k,t,c,d,x;
for(i=2;i<20000007;i++)
{
if(a[i]==0)
{
a[i]=2;
num++;
b[num]=i;
e[i]=1;
}
for(j=1;j<=num&&i*b[j]<20000007;j++)
{
a[i*b[j]]=1;
e[i*b[j]]=e[i]+1;
if(i%b[j]==0)
{
e[i*b[j]]--;
break;
}
}
}
// find(92);
cin>>t;
while(t--)
{
cin>>c>>d>>x;
for(i=1;i<=sqrt(x);i++)
{
if(x%i==0)
{
if((i+d)%c==0)
{
an+=pow(2,e[(i+d)/c]);
}
if((x/i+d)%c==0&&(i+d)/c!=(x/i+d)/c)
{
an+=pow(2,e[(x/i+d)/c]);
}
}
}
cout<<an<<endl;
an=0;
}
}