Codeforces Round #312 (Div. 2) ABC题解

【比赛链接】click here~~

A. Lala Land and Apple Trees:

【题意】:

AMR住在拉拉土地。拉拉土地是一个非常美丽的国家,位于坐标线。拉拉土地是与著名的苹果树越来越随处可见。

拉拉土地恰好n苹果树。树数i位于位置xi和具有人工智能的苹果就可以了增长。阿姆鲁希望从苹果树收集苹果。 AMR目前维持在X =0的位置。在开始的时候,他可以选择是否去左边或右边。他会在他的方向继续下去,直到他遇见一棵苹果树,他之前没有参观。他会采取所有的苹果,然后扭转他的方向,继续走这个方向,直到他遇到了另一个苹果树,他之前没有参观等。在换句话说,阿姆鲁访问每一个新的苹果树反转时他的方向。 AMR将停止收集苹果时,有没有更多的树木,他也没有在他所面对的方向参观。

求他可以收集的最大数量?

代码:

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
#define rep(i,j,k) for(int i=(int)j;i<(int)k;++i)
#define per(i,j,k) for(int i=(int)j;i>(int)k;--i)
#define lowbit(a) a&-a
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef unsigned long long LLU;
typedef double db;
const int N=1e6+10;
int  i,j,n,m,t,ans,res,cnt,tmp;

char str[N];
bool vis[N];
int nx[N],ny[N];

int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

struct node
{
    int x,y;
};

vector< pair<int,int> >ll,rr;

bool cmp1(pair<int ,int >a,pair <int ,int >b)
{
    return a.first>b.first;
}
bool cmp2(pair<int ,int >a,pair <int ,int >b)
{
    return a.first<b.first;
}
int main()
{
    while(cin>>n)
    {
        int x,a;
        rep(i,0,n)
        {
            cin>>x>>a;
            if(x<0) ll.push_back(make_pair(x,a));
            else rr.push_back(make_pair(x,a));
        }
        sort(ll.begin(),ll.end(),cmp1);
        sort(rr.begin(),rr.end(),cmp2);
        int res=0;
        int lenl=ll.size(),lenr=rr.size();
        if(lenl==lenr)
        {
            rep(i,0,lenl)
            {
                res+=ll[i].second;
                res+=rr[i].second;
            }
        }
        else if(lenl<lenr)
        {
            for(int i=0; i<lenl; ++i) res+=ll[i].second;
            for(int i=0; i<=lenl; ++i)  res+=rr[i].second;
        }
        else
        {
            for(int i=0; i<=lenr; ++i) res+=ll[i].second;
            for(int i=0; i<lenr; ++i)  res+=rr[i].second;
        }
        cout<<res<<endl;
    }
    return 0;
}

B. Amr and The Large Array

【题意】:

阿姆鲁得到了一个大阵大小为n的。AMR不喜欢大型阵列,所以他打算把它缩小。

AMR并不关心,除非它的美丽阵列中的任何东西。阵列的美被定义为次一些数发生在此数组的最大数目。他想选择这个阵列,使得它的美丽将是一样的原始数组中最小的子段。

帮助阿姆鲁通过选择最小的子段。

代码:

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
#define rep(i,j,k) for(int i=(int)j;i<(int)k;++i)
#define per(i,j,k) for(int i=(int)j;i>(int)k;--i)
#define lowbit(a) a&-a
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef unsigned long long LLU;
typedef double db;
const int N=1e6+10;
int  i,j,n,m,t,ans,res,cnt,tmp;

char str[N];
bool vis[N];
int nx[N],ny[N];

int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

struct node
{
    int x,y;
};

bool cmp1(node a,node b)
{
    return a.x<b.x;
}
pair<int,int> num [N];
int c[N];
int pre[N];
int last[N];
int maxxa=-1;
int maxxb=-1;

int main()
{
    cin>>n;
    rep(i,0,n)
    {
        cin>>j;
        ++c[j];
        maxxa=max(maxxa,c[j]);///找出那个数出现次数最多
        maxxb=max(maxxb,j);///那个数最大
        if(c[j]==1)
        {
            pre[j]=last[j]=i;/// pre[1]=last[1]=1
        }
        else
        {
            pre[j]=min(pre[j],i);
            last[j]=max(last[j],i);
        }
        // cout<<"pre[j]="<<j<<" "<<pre[j]<<endl;
        // cout<<"last[j]="<<j<<" "<<last[j]<<endl;
    }
    //cout<<maxxa<<endl;///3
    //cout<<maxxb<<endl;///2
    int fx=-1,fy=N;
    for(int i=0; i<=maxxb; ++i)
    {
        if(c[i]==maxxa)
        {
            if(last[i]-pre[i]<fy)
            {
                fx=pre[i];
                fy=last[i]-pre[i];
            }
        }
    }
    cout<<fx+1<<" "<<fx+fy+1;
    return 0;
}

C. Amr and Chemistry

【题意】

AMR热爱化学,以及专门做实验。他正在准备一个新的有趣的实验。

AMR有n个不同类型的化学品。每一化学i具有艾升的起始体积。对于该实验,阿姆鲁具有混合所有的化学品一起,但所有的化学品卷必须等于第一位。所以,他的任务就是使所有的化学品数量相等。

要做到这一点,阿姆鲁可以做两种不同的操作。

选择一些化学i和一倍当前音量,使新卷将2AI

选择一些化学i和除以2(整数除法)的体积,使新卷会

假设每个化学包含在无限量的容器。现在阿姆鲁想知道是什么操作,使所有化学品卷等于所需的最低人数?

代码:

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
#define rep(i,j,k) for(int i=(int)j;i<(int)k;++i)
#define per(i,j,k) for(int i=(int)j;i>(int)k;--i)
#define lowbit(a) a&-a
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
#define debug(x) cerr<<#x<<"="<<x<<endl;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
typedef long long ll;
const ll INF=1<<28;
const ll LINF=1ll<<61;
//My own input/output stream
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putsp()
#define putii(x,y) puti(x),putnum(y),putsp()
#define putiii(x,y,z) putii(x,y),putnum(z),putsp()
#define putsi(x) putnum(x),putendl()
#define putsii(x,y) puti(x),putnum(y),putendl()
#define putsiii(x,y,z) putii(x,y),putnum(z),putendl()
inline ll getnum()
{
    register ll r=0;
    register bool ng=0;
    register char c;
    c=getchar();
    while(c!='-'&&(c<'0'||c>'9'))c=getchar();
    if(c=='-')ng=1,c=getchar();
    while(c!=' '&&c!='\n')r=r*10+c-'0',c=getchar();
    if(ng)r=-r;
    return r;
}
template <class T> inline void putnum(T x)
{
    if(x<0)putchar('-'),x=-x;
    register short a[20]= {},sz=0;
    while(x>0)a[sz++]=x%10,x/=10;
    if(sz==0)putchar('0');
    for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
inline void putsp()
{
    putchar(' ');
}
inline void putendl()
{
    putchar('\n');
}
inline char mygetchar()
{
    register char c=getchar();
    while(c==' '||c=='\n')c=getchar();
    return c;
}
typedef long long LL;
typedef unsigned long long LLU;
typedef double db;
const int N=1e6+10;
int  i,j,n,m,t,ans,res,cnt,tmp;

char str[N];
bool vis[N];
int nx[N],ny[N];

int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

struct node
{
    int x,y;
};

bool cmp1(node a,node b)
{
    return a.x<b.x;
}
pair<int,int> num [N];
int c[N];
int pre[N];
int last[N];
int maxxa=-1;
int maxxb=-1;
int a[N],b[N];
vector <bool>v[N],vans;
int main()
{
    scanf("%d",&n);
    int kk=20;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        int j;
        for(j=1; j<20; j++)
        {
            if((a[i]>>(j-1))>0&&(a[i]>>j)==0)
            {
                break;
            }
        }
        for(int k=j-1; k>=0; k--)
        {
            v[i].PB((a[i]>>k)&1);
        }
        kk=min(kk,int(v[i].size()));
    }
    for(int i=0; i<kk; i++)
    {
        bool ok=1;
        for(int j=1; j<n; j++)
        {
            if(v[j][i]!=v[j+1][i])
            {
                ok=0;
                break;
            }
        }
        if(ok)vans.PB(v[1][i]);
        else break;
    }
    for(int i=1; i<=n; i++)
    {
        int j=vans.size();
        while(j<v[i].size()&&v[i][j]==0)
        {
            b[i]++;
            j++;
        }
    }
    int ans=99999999999;
    for(int i=0; i<30-int(vans.size()); i++)
    {
        int tans=0;
        for(int j=1; j<=n; j++)
        {
            tans+=int(v[j].size())-int(vans.size()+min(i,b[j]))+max(0,i-b[j]);
        }
        ans=min(ans,tans);
    }
    printf("%d\n",ans);
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-08 05:31:24

Codeforces Round #312 (Div. 2) ABC题解的相关文章

Codeforces Round #247 (Div. 2) ABC

Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431 代码均已投放:https://github.com/illuz/WayToACM/tree/master/CodeForces/431 A - Black Square 题目地址 题意: Jury玩别踩白块,游戏中有四个区域,Jury点每个区域要消耗ai的卡路里,给出踩白块的序列,问要消耗多少卡路里. 分析: 模拟水题.. 代码: /* * Author: illuz

# Codeforces Round #529(Div.3)个人题解

Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Repeating Cipher 传送门 题意:第一个字母写一次,第二个字母写两次,依次递推,求原字符串是什么 题解:1.2.3.4,非常明显的d=1的等差数列,所以预处理一个等差数列直接取等差数列的每一项即可 代码: #include<bits/stdc++.h> using namespace s

Codeforces Round #531 (Div. 3) ABCDE题解

Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividing 题意: 给一个数n,然后要求你把1,2.....n分为两个集合,使得两个集合里面元素的和的差的绝对值最小. 题解: 分析可以发现,当n%4==0 或者 n%3==0,答案为0:其余答案为1.之后输出一下就好了. 代码如下: #include <bits/stdc++.h> using name

Codeforces Round #540 (Div. 3) 部分题解

Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选一些题来写吧. B. Tanya and Candies 题意: 在n个数中任意删去一个,如果这个数被删去后,剩余数的奇数和以及偶数和相等,那么就定义这个数为"好数".现在问这n个数中有多少个“好数”. 题解: 预处理出奇数前缀和.偶数前缀和,删去一个数后所有的奇数位置和 就为前面的奇数和

Codeforces Round #366 (Div. 2) ABC

Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 1 #I hate that I love that I hate it 2 n = int(raw_input()) 3 s = "" 4 a = ["I hate that ","I love that ", "I hate it","I love it"] 5 fo

Codeforces Round #632 (Div. 2) 部分题解

目录 Codeforces Round #632 (Div. 2) A. Little Artem B. Kind Anton C. Eugene and an array D. Challenges in school №41 F. Kate and imperfection Codeforces Round #632 (Div. 2) A. Little Artem 题意:略. 分析:构造这样的图形: BWW...W BWW...W BBB...B #include <bits/stdc++

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

比赛链接:http://codeforces.com/contest/567 听说Round #Pi的意思是Round #314... A. Lineland Mail time limit per test:3 seconds memory limit per test:256 megabytes All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with it

Codeforces Round #306 (Div. 2) (ABCE题解)

比赛链接:http://codeforces.com/contest/550 A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and &

Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力

A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/problem/A Description Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous