Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) A - D2

A. Be Positive

链接:http://codeforces.com/contest/1130/problem/A

题意: 给一段序列,这段序列每个数都除一个d(1e3d1e3)除完后,如果正数的个数可以大于序列长度的一半输出这个d,否则输出0

思路: 直接找正数和负数的个数,正数个数大于一半输出1,负数个数大于一半输出-1,都不大于一半长度输出0就好了

代码:

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

int main()
{
    int n,x,a,b;
    a = 0;b = 0;
    cin>>n;
    for(int i = 1;i <= n;i ++){
        cin>>x;
        if(x>0) a++;
        else if(x<0) b++;
    }
    if(n%2==1) n = n/2+1;
    else n = n/2;
    if(a >= n) cout<<1<<endl;
    else if(b >= n) cout<<-1<<endl;
    else cout<<0<<endl;
}

B. Two Cakes

链接:http://codeforces.com/contest/1130/problem/B

思路:

每次只有两种取法,每次取局部最优就好了

实现代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
const int M = 1e5+10;
vector<ll>v[M];
int main()
{
    ll n,x;
    cin>>n;
    for(int i = 0;i < n*2;i ++){
        cin>>x;
        v[x].push_back(i);
    }
    ll ans = v[1][0]+v[1][1];
    for(int i = 1;i < n;i ++)
        ans += min(abs(v[i][0]-v[i+1][0])+abs(v[i][1]-v[i+1][1]),abs(v[i][0]-v[i+1][1])+abs(v[i][1]-v[i+1][0]));
    cout<<ans<<endl;
}

C. Connect

链接:http://codeforces.com/contest/1130/problem/C

题意:给一副地图,0代表陆地,1代表海洋,你只能在陆地上行动,两片陆地之间可以最多建一个通道,通道有花费,问从起点到终点最小的花费是多少

思路:因为图非常小,我们先判断下能否直接从起点到终点,如果不能那么代表起点和终点在两片陆地上,我们分别将两片陆地的所有点存到两个vector里,两重循环枚举一遍所有点,取最小的花费就好了

实现代码:

#include<bits/stdc++.h>
using namespace std;
const int M = 60;
const int inf = 0x3f3f3f3f;
int flag,n;
struct node{
    int x,y;
};
node s,t;
vector<node>v[3];
int vis[M][M];
char mp[M][M];
void dfs(int x,int y,int cnt){
    if(x > n||x < 1||y > n||y < 1) return;
    if(vis[x][y]||mp[x][y]=='1') return ;
    if(x==t.x&&y==t.y&&cnt==1) flag = 1;
    vis[x][y] = 1;
    node now; now.x = x;now.y = y;
    v[cnt].push_back(now);
    dfs(x+1,y,cnt); dfs(x-1,y,cnt); dfs(x,y+1,cnt); dfs(x,y-1,cnt);
    return ;
}

int main()
{
    cin>>n;
    cin>>s.x>>s.y;
    cin>>t.x>>t.y;
    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= n;j ++)
            cin>>mp[i][j];
    dfs(s.x,s.y,1);
    int mn = inf;
    if(flag) cout<<0<<endl;
    else{
        dfs(t.x,t.y,2);
        for(auto &i: v[1]){
            for(auto &j: v[2]){
                mn = min(mn,(i.x-j.x)*(i.x-j.x)+(i.y-j.y)*(i.y-j.y));
            }
        }
        cout<<mn<<endl;
    }
}

D2. Toy Train

链接:http://codeforces.com/contest/1130/problem/D2

题意:有从1到n的环状铁道路线,给出m个糖果,每个糖果有信息a,b,代表该糖果在a点要送到b点,问1到n依次为起点的情况下送完所有糖果要走多远

思路:因为要计算的是送完所有糖果需要多远,最优的送法肯定是将离自己最近糖果留到最后,所以我们只需要维护每个点拥有的糖果中需要送的地方离本身点最近的那个糖果,并且维护下每个点有多少颗糖果,因为对于每个点来说一圈才能送掉一颗糖果,最后依次枚举每个起点,看所有点最晚要多久送完,取最久的就好了。

D1和D2是一道题只不过D1的数据小一点,代码两道题都能过。

实现代码;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 6e3;
const int inf = 0x3f3f3f3f;
int n,m,a,b;
int cnt[M],mn[M];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int mx = 0,maxx= 0;
    cin>>n>>m;
    memset(mn,inf,sizeof(mn));
    for(int i = 1;i <= m;i ++){
        cin>>a>>b;
        if(b < a) b += n;
        if(b - a < mn[a]) mn[a] = b-a;
        cnt[a]++;
    }
    for(int i = 1;i <= n;i ++){
        maxx = 0;
        for(int j = 1;j <= n;j ++){
                if(cnt[j]){
                if(j < i) maxx = max(maxx,j+n-i+mn[j]+(cnt[j]-1)*n);
                else maxx = max(maxx,j-i+mn[j]+(cnt[j]-1)*n);
                }
        }
        cout<<maxx<<endl;
    }
    return 0;
}

 

posted @ 2019-02-25 22:02  冥想选手  阅读(162)  评论(0编辑  收藏  举报