Codeforces-542div2

http://www.javashuo.com/article/p-ekakfukt-eu.htmlhtml

codeforces-1130A~Gnode

和隊友作了一套題,,ios

A. Be Positive

題意

題意是給你一串整數,,要找到一個除數使得每個數被除後正數的個數大於等於 \(\lceil \frac{n}{2} \rceil\),,,c++

分析

統計出全部正數,負數的個數,,正數多那個除數就是1,負數多就是-1算法

代碼

//cf
#include <bits/stdc++.h>
//#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <string.h>
//#include <algorithm>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;//1061109567
const ll linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e6 + 5;
const int maxm = 2e5 + 5;
const ll mod = 1e9 + 7;
int a[maxn];
int main()
{
//    freopen("233.in" , "r" , stdin);
//    freopen("233.out" , "w" , stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i)cin >> a[i];
    sort(a + 1, a + 1 + n);
    int nump = 0;
    int numn = 0;
    for(int i = 1; i <= n; ++i)
        if(a[i] > 0)
            ++nump;
        else if(a[i] < 0)
            ++numn;
    if(nump >= (n + 1) / 2)
        cout << 1 << endl;
    else if(numn >= (n + 1) / 2)
        cout << -1 << endl;
    else
        cout << 0 << endl;
    return 0;
}

B. Two Cakes

題意

題意是由兩組1~n的數組成的序列,,每個人選擇一組,,費用是兩個樹之間的距離,,而後問你總距離最小是多少,,數組

分析

我一開始想着先貪心處理一我的的選擇出最少的,,再加上剩下的那我的的,,而後就wa了,,由於這樣並不保證這一次選的和下一次選的距離和是最小的,,解決方法是兩個一塊兒處理,,考慮每一種選擇的狀況,,這樣取最小的就好了,,,spa

代碼

//cf
#include <bits/stdc++.h>
//#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <string.h>
//#include <algorithm>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;//1061109567
const ll linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e6 + 5;
const int maxm = 1e4 + 5;
const ll mod = 1e9 + 7;
int a[maxn][2];
bool flag[maxn];
int main()
{
//    freopen("233.in" , "r" , stdin);
//    freopen("233.out" , "w" , stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n;cin >> n;
    int t;
    memset(flag, false, sizeof flag);
    for(int i = 1; i <= 2 * n; ++i)
    {
        cin >> t;
        if(!flag[t])
        {
            a[t][0] = i;
            flag[t] = true;
        }
        else
            a[t][1] = i;
    }
    ll ans = a[1][0] + a[1][1] - 2;
    for(int i = 1; i <= n - 1; ++i)
    {
        int p = abs(a[i + 1][0] - a[i][0]) + abs(a[i + 1][1] - a[i][1]);
        int q = abs(a[i + 1][0] - a[i][1]) + abs(a[i + 1][1] - a[i][0]);
        ans += min(p, q);
    }
    cout << ans << endl;
    return 0;
}

C. Connect

題意

給你一個地圖,,其中陸地是0,水則是1,,而後給你一個起點一個終點,,你能夠在任意兩塊陸地上建 一條 隧道使這兩片陸地相通,,而後問你起點到終點須要的隧道的最小長度,,,code

分析

由於只能建一條隧道,,因此若是起點所在的陸地與終點所在的陸地不相通的話,,那麼這條隧道必定在這兩片陸地之間,,數據量不大,,直接枚舉這兩片陸地上的點,,取最小的距離就好了,,,htm

判斷一個點是否在起點或終點所在的陸地能夠現用並查集把地圖 「染色」,,,這樣就能夠枚舉了,,,blog

代碼

//cf
#include <bits/stdc++.h>
//#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <string.h>
//#include <algorithm>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;//1061109567
const ll linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e6 + 5;
const int maxm = 2e5 + 5;
const ll mod = 1e9 + 7;
int fa[maxn];
int _find(int x)
{
    if(fa[x] == x)return x;
    return fa[x] = _find(fa[x]);
}
void _union(int x, int y)
{
    int f1 = _find(x);
    int f2 = _find(y);
    if(f1 != f2)fa[f1] = f2;
    else        fa[f2] = f1;
}
int mp[60][60];
int solve(int i, int j, int n)
{
    int x1 = i / n;
    int y1 = i - x1 * n;
    int x2 = j / n;
    int y2 = j - x2 * n;
    if(y1 == 0)
    {
        y1 = n;
        --x1;
    }
    if(y2 == 0)
    {
        y2 = n;
        --x2;
    }
//    cout << x1 << y1 << x2 << y2 << endl;
    return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
int main()
{
//    freopen("233.in" , "r" , stdin);
//    freopen("233.out" , "w" , stdout);
//    ios_base::sync_with_stdio(0);
//    cin.tie(0);cout.tie(0);
    int n;scanf("%d", &n);
    int x1, x2, y1, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    for(int i = 1; i <= n; ++i)
    {
        getchar();
        for(int j = 1; j <= n; ++j)
            mp[i][j] = (int)(getchar() - '0');

    }

    for(int i = n + 1; i <= n + 1 + n * n; ++i)fa[i] = i;
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            if(mp[i - 1][j] == mp[i][j] && i - 1 >= 1)
                _union(i * n + j, (i - 1) * n + j);
            if(mp[i + 1][j] == mp[i][j] && i + 1 <= n)
                _union(i * n + j, (i + 1) * n + j);
            if(mp[i][j + 1] == mp[i][j] && j + 1 <= n)
                _union(i * n + j, i * n + j + 1);
            if(mp[i][j - 1] == mp[i][j] && j - 1 >= 1)
                _union(i * n + j, i * n + j - 1);
        }
    }
//    for(int i = 1; i <=n; ++i)
//    {
//        for(int j = 1; j <= n; ++j)
//            cout << _find(i * n + j) << " ";
//        cout << endl;
//    }
    int s = _find(x1 * n + y1);
    int t = _find(x2 * n + y2);
//    cout << s << t << endl;
    int ans = inf;
    for(int i = n + 1; i <= n + 1 + n * n; ++i)
    {
        for(int j = 1 + n; j <= n + 1 + n * n; ++j)
        {
            if(_find(i) == s && _find(j) == t)
            {
                ans = min(ans, solve(i, j, n));
            }
        }
    }
    cout << ans << endl;
    return 0;
}

D1. Toy Train

題意

由一個環形的鐵路,,上面有n個車站,,每一個車站有一些糖果,,這些糖果要運到 \(b_i\) 那個車站,,,火車只能在一個車站拉上一個糖果,,可是能夠放下任意塊糖果,,,問你從這n個車站出發送完全部的糖果所需的最少的時間,,

分析

每次只能上一個糖果,,最後下的糖果就是糖果數量最多的車站的,,找一個從這個車站出發花費最多的另外一個車站,,這樣把那個車站全部的糖果送完時其餘車站的糖果順帶也就送完了,,,

枚舉每個車站i,,對於車站i枚舉全部的其餘的車站,,求出全部的時間裏的最大值就是這個車站所用的時間了,,,

參考

代碼

//cf
#include <bits/stdc++.h>
//#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <string.h>
//#include <algorithm>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;//1061109567
const ll linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e6 + 5;
const int maxm = 1e4 + 5;
const ll mod = 1e9 + 7;
struct node
{
    int num;
    int mi;
}node[maxm];
int n, m;
int getdis(int i, int j)
{
    //get the dis of i -> j
    if(i <= j)return j - i;
    else      return n - i + j;
}
int solve(int loc)
{
    //find the furthest and the most candies node
    int fur = loc;
    int num = node[loc].num;
    int ans = 0;
    int dis;
    for(int i = loc; i <= n; ++i)
    {
        if(node[i].mi == inf)continue;
        dis = getdis(loc, i) + (node[i].num - 1) * n + node[i].mi;
        ans = max(ans, dis);
    }
    for(int i = 1; i <= loc - 1; ++i)
    {
        if(node[i].mi == inf)continue;
        dis = getdis(loc, i) + (node[i].num - 1) * n + node[i].mi;
        ans = max(ans, dis);
    }
//    for(int i = loc; i <= n; ++i)
//    {
//        if(node[i].num >= num)
//        {
//            fur = i;
//            num = node[i].num;
//        }
//    }
//    for(int i = 1; i <= loc - 1; ++i)
//    {
//        if(node[i].num >= num)
//        {
//            fur = i;
//            num = node[i].num;
//        }
//    }
//    cout << fur << " ";
//    int ans = n * (node[fur].num - 1);
//    ans += getdis(loc, fur);
//    ans += getdis(fur, node[fur].mi);
    return ans;
}
int main()
{
//    freopen("233.in" , "r" , stdin);
//    freopen("233.out" , "w" , stdout);
//    ios_base::sync_with_stdio(0);
//    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    int a, b;
    for(int i = 1; i <= n; ++i)node[i].mi = inf;
    for(int i = 1; i <= n; ++i)node[i].num = 0;
    for(int i = 1; i <= m; ++i)
    {
        cin >> a >> b;
        ++node[a].num;
        if(getdis(a, b) <= node[a].mi)
            node[a].mi = getdis(a, b);
    }
    for(int i = 1; i <= n; ++i)
        cout << solve(i) << " ";
    cout << endl;
//    for(int i = 1; i <= n; ++i)
//    {
//        cout << i << " ";
//        cout << solve(i) << endl;
//    }
    return 0;
}

E. Wrong Answer

題意

一個數列求出最大的 區間和乘以區間長度,,

他給的算法當前面一段區間和出現負數就捨棄了,,沒有考慮長度對最後答案的影響,,,

題目要咱們構造一個數列,,使得這個數列的正確答案比它的作法算出的結果大k

分析

能夠構造一個前面1998個都是0,,後面一個數是-p,一個時p + q,,,

這樣正確的答案就是 \(2000q\),,,他算出的答案就是 \(p + q\),,,

要大k,,就是 \(2000q - (p+q)=k\),,也就是 \(q= \frac{p+k}{1999}\) ,,,爲了保證p,q都是整數,,,那麼就設 \(p=1999-k\%1999\),,這樣算出的q就是整數,,,

//cf
#include <bits/stdc++.h>
//#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <string.h>
//#include <algorithm>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;//1061109567
const ll linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e6 + 5;
const int maxm = 1e4 + 5;
const ll mod = 1e9 + 7;

int main()
{
//    freopen("233.in" , "r" , stdin);
//    freopen("233.out" , "w" , stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int k; cin >> k;
    cout << 2000 << endl;
    for(int i = 1; i <= 2000 - 2; ++i)cout << 0 << " ";
    int p = 1999 - k % 1999;
    cout << -p << " " << ((k + p) / 1999 + p) << endl;
    return 0;
}

(end)

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息