Codeforces Round #503 (by SIS, Div. 2) A 模拟 B搜索 C思维

A

#include <bits/stdc++.h>
#pragma comment(linker, “/STACK:1024000000,1024000000”)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int AX = 2e3+66;
int a[AX];
int main(){
    int n , h , a , b , k ;
    int x , y , f1 ,f2;
    scanf("%d%d%d%d%d",&n,&h,&a,&b,&k);
    while( k-- ){
        scanf("%d%d%d%d",&x,&f1,&y,&f2);
        LL res = 0LL;

        if( x == y ){
            printf("%d\n",abs(f1-f2));
            continue;
        }
        res += abs( y - x );
        if(f1 < f2) swap(f1,f2);
        if(f1 <= a) res += abs(a-f1)+abs(a-f2);
        else if( f1 <= b) res+=abs(f1-f2);
        else if(f1>b) res+=f1-b+abs(f2-b);

        cout << res << endl;
    }
    return 0 ;
}

B
Code:

#include <bits/stdc++.h>
#pragma comment(linker, “/STACK:1024000000,1024000000”)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int AX = 2e3+66;
int a[AX];
int vis[AX];
int f ;
std::vector<int> v[AX];
void dfs( int x ){
    vis[x] = 1;
    if( f ) return ; 
    for( int i = 0 ; i < v[x].size() ; i++ ){
        if( vis[v[x][i]] ){
            printf("%d ",v[x][i]);
            f = 1 ;
            return ;
        }else dfs( v[x][i] );
    }
}

int main(){
    int n;
    scanf("%d",&n);
    int x ;
    for( int i = 1 ; i <= n ; i++ ){
        scanf("%d",&x);
        v[i].push_back(x);
    }
    for( int i = 1 ; i <= n ; i++ ){
        f = 0 ;
        memset( vis , 0 ,sizeof(vis) );
        dfs( i );
    }
    return 0 ;
}

C
思路:枚举1号最后的得票数,然后对于比他多的人都要花钱买这些人的支持者
Code:

#include <bits/stdc++.h>
#pragma comment(linker, “/STACK:1024000000,1024000000”)
#define INF 0x3f3f3f3f3f3f3f3f
#define LL long long
using namespace std;
const int AX = 4e3+66;
std::vector<LL> v[AX];
LL b[AX];
LL  n , m ;
LL solve( int x , int left ){
    LL ans = 0LL;
    int cnt = 0 ;
    for( int i = 2 ; i <= m ; i++ ){
        int biger = 0 ;
        if( v[i].size() >= x ) biger = v[i].size() - x + 1 ;
        if( biger > left ) return INF;
        left -= biger;
        for( int j = 0 ; j < biger ; j++ ){
            ans += v[i][j];
        }
        for( int j = biger ; j < v[i].size() ; j++ ){
            b[++cnt] = v[i][j];
        }
    }
    sort( b + 1 , b + cnt + 1 );
    for( int i = 1 ; i <= left ; i ++ ){
        ans += b[i];
    }
    return ans ;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    LL res = INF ;
    //scanf("%d%d",&n,&m);
    cin >> n >> m ;
    int x  ;
    LL y ;
    for( int i = 1 ; i <= n;  i++ ){
        cin >> x >> y ;
        v[x].push_back(y);
    }
    for( int i = 1 ; i <= m ; i++ ){
        sort( v[i].begin() , v[i].end() );
    }
    //int lim = n / 2 + 1 ;
    // lim = n ;
    for( int i = v[1].size() ; i <= n ; i++ ){
        res = min( res , solve( i , i - v[1].size() ) );
    }
    cout << res << endl;
    return 0 ;
}

猜你喜欢

转载自blog.csdn.net/FrankAx/article/details/81603845
今日推荐