Codeforces Round #533 (Div. 2)

A. Salem and Sticks

由于长度很小,所以直接暴力枚举最后的长度即可,取最小值即可。

#include<bits/stdc++.h>
#define CLR(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=10010;
int a[1100],n;
int cost,ans;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int co=inf,ans=0;
    for(int i=1;i<=100;i++)
    {
        int tep=0;
        for(int j=1;j<=n;j++)
        {
            if(a[j]>=i+1)tep+=a[j]-1-i;
            else if(a[j]<=i-1)tep+=i-1-a[j];
        }
        if(tep<co){
            co=tep,ans=i;
        }
    }

    cout<<ans<<" "<<co<<endl;
}

B - Zuhair and Strings

用数组模拟栈,水题,要读清题意。

#include<bits/stdc++.h>
#define CLR(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=20010;
int n;
int a[200],ans[200];
int main(){
    string s;
    int k;
    cin>>n>>k;
    cin>>s;
    for(int i=0;i<n;i++)
    {
        if(a[s[i]]==0){
            CLR(a,0);

        }
        a[s[i]]++;
        ans[s[i]]+=a[s[i]]/k;
        a[s[i]]%=k;
    }
    int maxx=0;
    for(int i=‘a‘;i<=‘z‘;i++)
    {
        maxx=max(maxx,ans[i]);
    }
    cout<<maxx<<endl;
}

C. Ayoub and Lost Array

先预处理出 [ l , r ] ,所有除以3余0,余1,余2的个数,然后dp[ s ][ i ]表示到第 i 位,和的余数为 s 的方案个数,dp式子也很好列,看代码就行了。

#include<bits/stdc++.h>
#define CLR(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=200010;
ll p=1e9+7;
ll dp[3][maxn],l,r;
int n;
ll a[3];
int main(){
    cin>>n>>l>>r;
    while(l<=r&&(r-l+1)%3!=0){
        a[l%3]++;
        l++;
    }

    a[0]+=(r-l+1)/3;
    a[1]+=(r-l+1)/3;
    a[2]+=(r-l+1)/3;
    dp[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        dp[0][i]=((dp[0][i-1]*a[0]%p)+(dp[1][i-1]*a[2]%p)+(dp[2][i-1]*a[1]%p))%p;
        dp[1][i]=((dp[0][i-1]*a[1]%p)+(dp[1][i-1]*a[0]%p)+(dp[2][i-1]*a[2]%p))%p;
        dp[2][i]=((dp[0][i-1]*a[2]%p)+(dp[1][i-1]*a[1]%p)+(dp[2][i-1]*a[0]%p))%p;
    }
    printf("%lld\n",dp[0][n]);
}

D. Kilani and the Game

这个题意看的我自闭,看别人的题解才知道自己读错题了,还好打的不是现场的,不然又要掉分了。

题目的意思是有p种人,对于每个人,他可以走到距离 s 范围内的所有格子,而且是可以分身的,走过的地方全部被这个人污染(造起堡垒),然后问各种人污染了几个格子。

如果只有一种人的话,这个bfs只需要处理那些已经走过的区域的最外围就可以了,因为里面那些被包围的肯定没有外围的走的远,最外围的方块就是那些用光了步数才走到的格子,所以这里需要一个bfs。

现在有很多人,并且要依次执行,所以每种人都要用一个队列bord来保存最外围的格子,然后对一种人进行bfs的时候,把这个队列里的所有元素全部倒入主要的bfs队列q,然后对q进行bfs,把步数用光的那些格子塞回bord。

#include<bits/stdc++.h>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1010;
int n,m,p;
int vis[maxn][maxn];
int ans[20],s[20];
char mp[maxn][maxn];
int fx[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node{
    int x,y,step;
};
queue<node >q;
queue<node >bord[10];
bool check(int x,int y){
    if(x<1||x>n||y<1||y>m)return false;
    if(mp[x][y]==‘#‘)return false;
    if(vis[x][y]>0)return false;
    return true;
}
void bfs(int id)
{
    node st,ne;
    while(!q.empty()){
        st=q.front();
        q.pop();
        if(st.step==0){
            bord[id].push({st.x,st.y,s[id]});
            continue;
        }
        for(int i=0;i<4;i++)
        {
            ne=st;
            ne.step--;
            ne.x+=fx[i][0],ne.y+=fx[i][1];

            if(check(ne.x,ne.y)){
                vis[ne.x][ne.y]=id;
                q.push(ne);

            }
        }
    }
}
int expend(int id){
    while(!q.empty())q.pop();
    while(!bord[id].empty()){
        q.push(bord[id].front());
        bord[id].pop();
    }
    bfs(id);
    return !bord[id].empty();
}
int main(){
    cin>>n>>m>>p;
    for(int i=1;i<=p;i++)scanf("%d",&s[i]);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",mp[i]+1);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int id=mp[i][j]-‘0‘;
            if(id>0&&id<=9)
            {
                bord[id].push({i,j,s[id]});
                vis[i][j]=id;
            }
        }
    }
    int isok=1;
    while(isok)
    {
        isok=0;
        for(int i=1;i<=p;i++)
        {
            isok|=expend(i);
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            ans[vis[i][j]]++;
        }
    }
    for(int i=1;i<=p;i++)
    {
        printf("%d%c",ans[i],i<p?‘ ‘:‘\n‘);
    }
} 

E. Helping Hiasat

待补

原文地址:https://www.cnblogs.com/mountaink/p/10326198.html

时间: 2024-08-30 16:30:30

Codeforces Round #533 (Div. 2)的相关文章

Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)

题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 1 ~ p 的顺序,问最后每个人占领的点的数量. 题解:用一个队列维护当前起点,用另一个队列模拟当前起点走 a_i 步可以到达的全部点.(60 ~ 63行关键代码,多源bfs,不可分开跑) 数据: 4 3 2 2 1 1.. 1.. ..2 ... output:10 2 1 #include <

Codeforces Round #533 (Div. 2) B. Zuhair and Strings 【模拟】

传送门:http://codeforces.com/contest/1105/problem/B B. Zuhair and Strings time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Given a string ss of length nn and integer kk (1≤k≤n1≤k≤n). The string

Codeforces Round #533 (Div. 2)C. Ayoub and Lost Array

C. Ayoub and Lost Array Ayoub had an array ?? of integers of size ?? and this array had two interesting properties: All the integers in the array were between ?? and ?? (inclusive). The sum of all the elements was divisible by 3. Unfortunately, Ayoub

Codeforces Round #533 (Div. 2)E. Helping Hiasat_求最大独立子集转换求最大团

题目链接:E. Helping Hiasat 题解:把夹在同一段1里面的人互相连边,然后问题就转换为求最大独立子集的大小,最大独立子集大小等于补图的最大团.最大图不会的可以看这篇博客,我也是看这个的 #include<bits/stdc++.h> #include<iostream> #include<string> #include<cstring> #include<algorithm> #define pb push_back #defin

Codeforces Round #533 (Div. 2) Solution

A. Salem and Sticks 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 1010 5 int n, a[N]; 6 7 int work(int x) 8 { 9 int res = 0; 10 for (int i = 1; i <= n; ++i) 11 res += max(0, abs(x - a[i]) - 1); 12 return res; 13 } 14 15 int m

Codeforces Round #533 (Div. 2) ABCD 题解

题目链接 A. Salem and Sticks 分析 暴力就行,题目给的n<=1000,ai<=100,暴力枚举t,t从2枚举到98,复杂度是1e5,完全可行. 代码 1 #include <cstdio> 2 #include <cmath> 3 #include <iostream> 4 #include <cstring> 5 #include <algorithm> 6 #include <vector> 7 #

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我