Codeforces Round 734

news/2024/7/7 20:04:40

Portal.

B1. Wonderful Coloring - 1

Portal.

c n t i cnt_i cnti 代表第 i i i 种字母在 s s s 中的出现次数,有两种情况:

  1. c n t i > 1 cnt_i>1 cnti>1,此时对于这种字母,我们将它出现的某次染成红色,某次染成绿色,其他的出现情况不染色
  2. c n t i = 1 cnt_i=1 cnti=1,此时需要和另一个 c n t j = 1 cnt_j=1 cntj=1 的字母配对染成一红一绿。
#include <bits/stdc++.h>
using namespace std;

int cnt[30];

void solve()
{
    string s;cin>>s;
    memset(cnt,0,sizeof cnt);
    for(int i=0;i<s.length();i++) cnt[s[i]-'a']++;
    int cnt1=0,cnt2=0;
    for(int i=0;i<26;i++) 
    {
        if(cnt[i]==1) cnt1++;
        else if(cnt[i]>1) cnt2++;
    }
    cout<<(cnt1/2+cnt2)<<'\n';
}

int main()
{
    int t;cin>>t;
    while(t--) solve();
    return 0;
}

B2. Wonderful Coloring - 2

Portal.

和 B1 相似的思路,统计每种字母出现的次数,出现次数大于 k k k 的填成 k k k 种颜色,否则就都可以填色。

由于需要输出方案数,按照序列中数的大小排序,保证一样的数排在一起,记录原 id 按序输出即可。

#include <bits/stdc++.h>
using namespace std;

const int maxn=2e5+5;
int cnt[maxn],ans[maxn],ct[maxn];
struct node{int id,v;}a[maxn],b[maxn];

bool cmp(node a,node b)
{return a.v<b.v;}

void solve()
{
    int n,k;cin>>n>>k;
    for(int i=0;i<=n;i++) cnt[i]=0,ans[i]=0,ct[i]=0;
    for(int i=1;i<=n;i++) cin>>a[i].v,cnt[a[i].v]++,a[i].id=i;
    int tmp=0;
    for(int i=1;i<=n;i++) 
    {
        if(cnt[a[i].v]<k) b[++tmp]=a[i];
        else if(cnt[a[i].v]>=k&&ct[a[i].v]<k) ct[a[i].v]++,ans[a[i].id]=ct[a[i].v];
    }
    sort(b+1,b+tmp+1,cmp);
    int tp=1,tot=tmp/k*k;
    for(int i=1;i<=tot;i++)
    {
        ans[b[i].id]=tp,tp++;
        if(tp>k) tp=1;
    }
    for(int i=1;i<=n;i++) cout<<ans[i]<<' ';
    cout<<'\n';
}

int main()
{
    int t;cin>>t;
    while(t--) solve();
    return 0;
}

C. Interesting Story

Portal.

sol.

D1. Domino (easy version)

Portal.

sol.

D2. Domino (hard version)

Portal.

sol.

E. Fixed Points

设计状态 f ( i , j ) f(i,j) f(i,j) 表示对于前 i i i 个数,删去 j j j 个数后,满足 b i = i b_i=i bi=i 的条件个数。

对于第 i i i 个位置,考虑删 / 不删,转移如下:
f ( i , j ) = max ⁡ ( f ( i − 1 , j − 1 ) , f ( i − 1 , j ) + [ a i = i − j ] ) f(i,j)=\max(f(i-1,j-1),f(i-1,j)+[a_i=i-j]) f(i,j)=max(f(i1,j1),f(i1,j)+[ai=ij])
答案即为满足 f ( n , i ) ≥ k f(n,i)\geq k f(n,i)k 的最小的 i i i

#include <bits/stdc++.h>
using namespace std;

int a[2005],f[2005][2005];

void solve()
{
    int n,k;cin>>n>>k;
    for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) f[i][j]=0;
    for(int i=1;i<=n;i++) cin>>a[i],f[i][0]=f[i-1][0]+(a[i]==i);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) f[i][j]=max(f[i-1][j-1],f[i-1][j]+(a[i]==i-j));
    for(int i=0;i<=n;i++) if(f[n][i]>=k) return cout<<i<<'\n',void();
    cout<<"-1\n";
}

int main()
{
    int t;cin>>t;
    while(t--) solve();
    return 0;
}

F. Equidistant Vertices

Portal.

sol.


http://lihuaxi.xjx100.cn/news/1721044.html

相关文章

2022年12月 Python(一级)真题解析#中国电子学会#全国青少年软件编程等级考试

Python等级考试(1~6级)全部真题・点这里 一、单选题(共25题,每题2分,共50分) 第1题 关于Python语言的注释,以下选项中描述错误的是?( ) A: Python语言有两种注释方式:单行注释和多行注释 B: Python语言的单行注释以#开头 C: Python多行注释使用###来做为标记 D: …

postgresSQL 数据库本地创建表空间读取本地备份SQL文件

使用pgAdmin4&#xff0c;你安装PG得文件夹****/16/paAdmin 4 /runtime/pgAdmin4.exe 第一步&#xff1a;找到Tablespaces 第二步&#xff1a;创建表空间名称 第三步&#xff1a;指向数据文件 第四步&#xff1a;找到Databases&#xff0c;创建表空间 第五步&#xff1a;输入数…

身份证号码,格式校验:@IdCard(Validation + Hutool)

目标 自定义一个用于校验 身份证号码 格式的注解IdCard&#xff0c;能够和现有的 Validation 兼容&#xff0c;使用方式和其他校验注解保持一致&#xff08;使用 Valid 注解接口参数&#xff09;。 校验逻辑 有效格式 符合国家标准。 公民身份号码按照GB11643&#xff0d;…

SpringMVC Day 03 : 处理静态资源

前言 欢迎来到第三天的 SpringMVC 学习系列&#xff01;在前两天的教程中&#xff0c;我们已经学习了如何搭建 SpringMVC 环境、创建控制器和处理请求等基础知识。今天&#xff0c;我们将继续探索 SpringMVC 的功能&#xff0c;并学习如何处理静态资源。 在现代 Web 应用程序…

【shell】read -t -n1

if read -t 5 -p "Please enter your name:" name thenecho "Hello, $name, welcome to my script" else#起到换行的作用echo#输入计数 -n1read -n1 -p "Do you want to continue [Y/N]?" answercase $answer inY | y) echoecho "Fine, co…

UML中类之间的六种主要关系

UML中类之间的六种主要关系: 继承&#xff08;泛化&#xff09;&#xff08;Inheritance、Generalization&#xff09;, 实现&#xff08;Realization&#xff09;&#xff0c;关联&#xff08;Association)&#xff0c;聚合&#xff08;Aggregation&#xff09;&#xff0c;组…

英伟达携手联发科打造CPU,威胁英特尔主导地位 | 百能云芯

据路透社报道&#xff0c;英伟达&#xff08;NVIDIA&#xff09;计划采用安谋&#xff08;Arm&#xff09;架构设计中央处理器&#xff08;CPU&#xff09;&#xff0c;进军个人电脑&#xff08;PC&#xff09;市场。外界已指出&#xff0c;联发科是英伟达的合作伙伴&#xff0…

【shell】read -s

read -s -p "Please enter your password: " pass#添加了-s选项之后&#xff0c;不会自动换行&#xff0c;不添加-s 会自动换行 echo echo "Is your password really $pass?"