Codeforces Round #516 (Div. 1) 题解

A.Oh Those Palindromes

直接排序之后输出就行了。

证明的话直接跟据同一种字符最多能在多少个回文串中贡献答案就行了。

#include <bits/stdc++.h>
#define for1(a,b,i) for(int i=a;i<=b;++i)
#define FOR2(a,b,i) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
#define M 500005
int n;
char s[M];
int cnt[M];

int main () {
    //freopen("a.in","r",stdin);
    scanf("%d%s",&n,s+1);
    for1(1,n,i) ++cnt[s[i]-‘a‘];
    for1(0,25,i) for1(1,cnt[i],j) putchar(‘a‘+i);
    puts("");
}

B.Labyrinth

显然如果向左走的多,向右走一定多,直接最短路就行了。

#include <bits/stdc++.h>
#define for1(a,b,i) for(int i=a;i<=b;++i)
#define FOR2(a,b,i) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
#define M 2005
int n,m;
char s[M];
bool vis[M][M];
int ax,ay,x_,y_,a[M][M];
int f[4]={0,0,1,-1},g[4]={1,-1,0,0};

struct node {int x,y;}dis[M][M];

int main () {
//    freopen("a.in","r",stdin);
    scanf("%d%d%d%d%d%d",&n,&m,&ax,&ay,&x_,&y_);
    for1(1,n,i) {
        scanf("%s",s+1);
        for1(1,m,j) a[i][j]=s[j]==‘.‘;
    }

    queue <node> q;
    q.push((node){ax,ay});
    for1(1,n,i) for1(1,m,j) dis[i][j].x=2e9,dis[i][j].y=2e9;
    dis[ax][ay]=(node){0,0};
    while (!q.empty()) {
        node t=q.front();
        q.pop();
        vis[t.x][t.y]=0;
        FOR2(3,0,i) {
            int tox=t.x+f[i];
            int toy=t.y+g[i];
            if(!tox||!toy||tox>n||toy>m||!a[tox][toy]) continue;
            if(dis[tox][toy].x<=dis[t.x][t.y].x+(i==1)) continue;
            dis[tox][toy]=dis[t.x][t.y];
            if(i==0) ++dis[tox][toy].y;
            if(i==1) ++dis[tox][toy].x;
            if(!vis[tox][toy]) vis[tox][toy]=1,q.push((node){tox,toy});
        }
    }
    int cnt=0;
    for1(1,n,i) for1(1,m,j) if(a[i][j]) {
        cnt+=dis[i][j].x<=x_&&dis[i][j].y<=y_;
    }
    cout<<cnt<<endl;
}

C.Dwarves,Hats and Extrasensory Abilities

直接找个轴二分点就行了。

注意最后输出直线的时候就不能过这个轴上的整点了,会重。

#include <bits/stdc++.h>
#define for1(a,b,i) for(int i=a;i<=b;++i)
#define FOR2(a,b,i) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
inline int read() {
    int f=1,sum=0;
    char x=getchar();
    for(;(x<‘0‘||x>‘9‘);x=getchar()) if(x==‘-‘) f=-1;
    for(;x>=‘0‘&&x<=‘9‘;x=getchar()) sum=sum*10+x-‘0‘;
    return f*sum;
}

#define M 1000005
int n;
char s[10];

int main () {
    //freopen("a.in","r",stdin);
    cin>>n;
    cout<<"1 0"<<endl;
    cin>>(s+1);
    int co=s[1];
    int l=0,r=1e9,mid;
    for1(2,n,i) {
        mid=l+r>>1;
        cout<<1<<" "<<mid<<endl;
        cin>>(s+1);
        if(s[1]==co) l=mid;
        else r=mid;
    }
    mid=l+r>>1;
    cout<<0<<" "<<l<<" "<<2<<" "<<r<<endl;
}
/*
30
0 1 1 0 1 1 0 0 0 0
1 0 0 0 1 1 0 1 0 1
0 0 0 1 1 1 0 1 1 1
*/

D.Candies for Children

没想到可以按照$sqrt(n)$分类。

就是吃两个的个数和吃的轮数是相互限制的。

所以就分这个讨论就好了,最后一个人可以吃不完直接$++s$然后强制最后一个人吃两个就行了。

#include <bits/stdc++.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define for1(a,b,i) for(int i=a;i<=b;++i)
#define FOR2(a,b,i) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;

ll n,k,x,y;

int main () {
//    freopen("a.in","r",stdin);
    cin>>n>>x>>y>>k;
    x=y-x+1+(y<x?n:0);
    y=n-x;

    if(x>k) return puts("-1"),0;
    if(k/n>n) {
        if(!y) swap(x,y);
        int ans=-1;
        for1(0,n,i) {
            int s=k%(n+i);
            if(s>=x&&s<=2*x&&i-s+x>=0&&i-s+x<=y) ans=i;
        }
        ++k;
        for1(ans+1,n,i) {
            int s=k%(n+i);
            if(s>x&&s<=2*x&&i-s+x>=0&&i-s+x<=y) ans=i;
        }
        printf("%d\n",ans);
    }
    else {
        ll ans=-1;
        if(k<=2*x) ans=min(k-x+1,x)+y;
        FOR2((k-x)/n,1,i) {
            ll c=k-x-n*i;
            ll a=c,b=-c,t;
            t=(c-1)/(i+1)+1;
            a-=i*t,b+=(i+1)*t;
            if(a<0||b>y) continue;
            t=min((y-b)/(i+1),a/i);
            a-=i*t,b+=(i+1)*t;
            if(a<=x) ans=a+b;
        }
        ++k;
        FOR2((k-x)/n,1,i) {
            ll c=k-x-n*i;
            ll a=c,b=-c,t;
            t=(c-1)/(i+1)+1;
            a-=i*t,b+=(i+1)*t;
            if(a<=0||b>y) continue;
            t=min((y-b)/(i+1),(a-1)/i);
            a-=i*t,b+=(i+1)*t;
            //这里取max啊-_-
            if(a<=x) ans=max(ans,a+b);
        }
        printf("%lld\n",ans);
    }
}

原文地址:https://www.cnblogs.com/asd123www/p/9851254.html

时间: 2024-08-01 21:41:24

Codeforces Round #516 (Div. 1) 题解的相关文章

Codeforces Round #262 (Div. 2) 题解

A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When

Codeforces Round #FF (Div. 2) 题解

比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n 

Codeforces Round #259 (Div. 2) 题解

A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n?>?1) is an n?×?n 

Codeforces Round #177 (Div. 2) 题解

[前言]咦?现在怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛,一般我报一个数字A,随便再拉一个人选一个数字B.然后开始做第A^B场.如果觉得机密性不高,来点取模吧.然后今天做的这场少有的AK了.(其实模拟赛只做完了4题,最后1题来不及打了) 等等,话说前面几题不用写题解了?算了,让我难得风光一下啦. [A] A. Polo the Penguin and Segments time limit per test 2 seconds mem

Codeforces Round #534 (Div. 2)题解

Codeforces Round #534 (Div. 2)题解 A. Splitting into digits 题目大意 将一个数字分成几部分,几部分求和既是原数,问如何分可以使得分出来的各个数之间的差值尽可能小 解题思路 将n分成n个1相加即可 AC代码 #include<cstring> #include<string> #include<iostream> #include<cstdio> using namespace std; int main

Codeforces Round #516 (Div. 2, by Moscow Team Olympiad)

Codeforces Round #516 (Div. 2, by Moscow Team Olympiad) https://codeforces.com/contest/1064 A 1 #include<bits/stdc++.h> 2 #define pb push_back 3 using namespace std; 4 5 bool Check(int a,int b,int c){ 6 if(a+b>c&&b+c>a&&a+c>

Codeforces Round #561 (Div. 2) 题解

Codeforces Round #561 (Div. 2) 题解 题目链接 A. Silent Classroom 水题. Code #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 105; int n; char s[N], t[N]; int main() { cin >> n; for(int i = 1; i <= n; i++) { scanf(&q

Codeforces Round #608 (Div. 2) 题解

目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 程序 D. Portals 题意 做法 程序 E. Common Number 题意 做法 程序 结束语 Codeforces Round #608 (Div. 2) 题解 前言 题目链接:仅仅只是为了方便以题目作为关键字能查找到我的题解而已(逃 Codeforces 1271A Codeforce

Codeforces Round #617 (Div. 3) 题解

目录 Codeforces Round #617 (Div. 3) 题解 前言 A. Array with Odd Sum 题意 做法 程序 B. Food Buying 题意 做法 程序 C. Yet Another Walking Robot 题意 做法 程序 D. Fight with Monsters 题意 做法 程序 E1. String Coloring (easy version) 题意 做法 程序 E2. String Coloring (hard version) 题意 做法