【Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)】 A B C D1 D2 E

A

题意 给你n个数 让你除以一个数 使得正数的个数大于一半

我们很容易相当 除以1 和负一满足题意 其中1不改变正负性 -1改变正负性 那么我们只要统计正数和负数的个数即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 125;
int arr[MAX_N];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,check,num=0,num_= 0;
    scanf("%d",&n);
    if(n%2==1) check = n/2+1;
    else check = n / 2;
    for(int i = 1;i<=n;++i)
    {
        scanf("%d",&arr[i]);
        if(arr[i]>0) num++;
        else if(arr[i]<0) num_++;
    }
    if(num>=check) printf("1\n");
    else if(num_>=check) printf("-1\n");
    else printf("0\n");
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


B

题意 给你2*n个蛋糕 两个人按1-n顺序买 问你最短距离 

和camp一道题很像 实际上不用考虑谁买哪个 因为对总的距离来说两种人怎么买都是一样的

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 100025;
int arr[MAX_N*2];
vector<int > vt[MAX_N];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;

    scanf("%d",&n);
    for(int i = 1;i<=2*n;++i)   scanf("%d",&arr[i]),vt[arr[i]].push_back(i);
    long long ans = abs(vt[1][0]-1)+abs(vt[1][1]-1);
    for(int i = 2;i<=n;++i)
        ans+=abs(vt[i][0]-vt[i-1][0])+abs(vt[i][1]-vt[i-1][1]);
    printf("%lld\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


C

题意 给你好多陆地和海洋 问你起点和终点怎么到达距离最短 trick 是 只能连一条边 边贡献为 x距离差的平方 和y距离差的平方 如果不连一条边难度会上升

那么我们只暴力枚举连边即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
char MP[55][55];
vector<pair<int ,int> > vt[2500];
bool flag[55][55];
int tot,dir[4][2]={0,-1,0,1,1,0,-1,0},n;
map<int ,int > mp;
void dfs(int x,int y)
{
    mp[(x-1)*n+y] = tot;
    vt[tot].push_back(make_pair(x,y));
    for(int i = 0;i<4;++i)
    {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&!flag[xx][yy]&&MP[xx][yy]=='0')
        {
            flag[xx][yy] = true;
            dfs(xx,yy);
        }
    }
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int sx,sy,ex,ey;
    int ans = 0x3f3f3f3f;
    scanf("%d",&n);
    scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
    for(int i = 1;i<=n;++i)
        scanf("%s",MP[i]+1);
    for(int i = 1;i<=n;++i)
    {
        for(int j  = 1;j<=n;++j)
        {
            if(MP[i][j]=='0'&&!flag[i][j]) flag[i][j] = true,tot++,dfs(i,j);
        }
    }
    int t = (sx-1)*n+sy,t_ = (ex-1)*n+ey;
    if(mp[t]==mp[t_]) printf("0\n");
    else
    {

        int sz = vt[mp[t]].size(),sz_ = vt[mp[t_]].size();
        for(int i = 0;i<sz;++i)
        {
            for(int j = 0;j<sz_;++j)
            {
                ans = min(ans,(vt[mp[t]][i].first-vt[mp[t_]][j].first)*(vt[mp[t]][i].first-vt[mp[t_]][j].first)+(vt[mp[t]][i].second-vt[mp[t_]][j].second)*(vt[mp[t]][i].second-vt[mp[t_]][j].second));
            }
        }
        printf("%d\n",ans);
    }
    
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

D2

D1 D2同题 所以说下D2

题意 小火车按照1-n - 1 -n的顺序开车 在一个站可以一次装一个物品 和放一个物品 装物品和放物品不计时间 但是开车一个距离一个时间

问你从每个点开始要送所有货物 最短时间是多少

做法 我们只要先枚举起点 那么你j这个点和我起点就是 dis(i,j) 然后我们进行 cnt[j] -1 次n循环就 可以解决cnt[j]-1个货物 然后贪心的取最近的货物 当做最后一次送即可 那么你只要第一个循环遍历开始的起点 第二个循环暴力找完成的最大时间 也是必须要的时间因为必须都满足(为什么取max 因为你要最多的那个次数满足才可以 max不是max本意

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
int cnt[5025],minn[5025];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int a,b,n,m,maxx=0,maxx_;
    scanf("%d%d",&n,&m);
    memset(minn,0x3f,sizeof(int)*(n+1));
    for(int i = 1;i<=m;++i)
    {
        scanf("%d%d",&a,&b);
        cnt[a]++,maxx = max(maxx,cnt[a]);
        minn[a] = min(minn[a],(b-a+n)%n);
    }
    for(int i = 1;i<=n;++i)
    {
        int st = 0;
        long long tmp,ans = 0;
        maxx_ = 0;
        for(int j = 1;j<=n;++j)
        {
            tmp = 0;
            if(cnt[j]==0) continue;
            tmp+=(j-i+n)%n+(cnt[j]-1)*n+minn[j];
            dbg(tmp);
            ans = max(tmp,ans);
        }
        printf("%lld ",ans);
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


E

题意 这个人他很贪心 他找到一段子序列和最大的就当做答案乘起来

但是我们知道 有时候长度的贡献很很大 所以我们让长度直接为2000 这样的话 只要考虑一个式子 (p+q)*2000 = p + 2000

其中q是负数即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;

int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    long long x,p,q;
    bool flag = false;
    scanf("%lld",&x);
    if(x%2000==0) flag = true;
    printf("2000\n");
    for(int i = 1;i<=1998;++i)
        printf("0 ");
    if(flag)
    {
        long long tmp = x/2000;
        for(p = 2000;p<=1000000;p+=2000)
        {
            if(tmp+p/2000-p<0)
            {
                q = tmp + p/2000-p;
                break;
            }
        }
    }
    else
    {
        for(p = 2000;p<=1000000;p+=2000)
        {
        long long tmp = p - x%2000;
        if((tmp+x)/2000-tmp<0)
        {a
            q = (tmp+ x)/2000-tmp;
            p = tmp;
            break;
        }
        }
    }
    printf("%lld %lld\n",q,p);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/88636683