03.10訓練賽題解與心得(最近松懈+浮躁=爆零)

最近腦子抽的厲害,東學一點,西補一點,著急+浮躁+粗心=爆零啊。

A - Question 1

SPOJ - SERGRID
這就道BFS模板題,沒啥好說的,清醒后一發過。。。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
#define LL long long
#define MAXN 505
#define CLR(x) memset(x, 0, sizeof(x))
char ss[MAXN][MAXN];
int land[MAXN][MAXN];
bool vis[MAXN][MAXN];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m;

struct node{
    int x, y;
    int step;
};

int BFS()
{
    queue <node> q;
    node now;
    now.x = 0; now.y = 0; now.step = 0;
    q.push(now);
    memset(vis, false, sizeof(vis));
    vis[now.x][now.y] = true;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        if (now.x == n-1 && now.y == m-1) return now.step;
        int r, c;
        for (int i = 0; i < 4; i++)
        {
            int cnt = land[now.x][now.y];/*可以跳躍的步數*/
            r = now.x + cnt*dx[i];
            c = now.y + cnt*dy[i];
            if (r<n && r>=0 && c<m && c>=0 && !vis[r][c])
            {
                vis[r][c] = true;
                node next;
                next.x = r;
                next.y = c;
                next.step = now.step + 1;
                q.push(next);
            }
        }
    }
    return -1;
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++)
    {
        getchar();
        for (int j = 0; j < m; j++)
        {
            scanf("%c", &ss[i][j]);
            land[i][j] = ss[i][j] - '0'; //注意數字是連續輸入的,要處理一下
        }
    }
    int res = BFS();
    printf("%d", res);
    return 0;
}

B - Question 2

SPOJ - IAPCR2F **
這題。。并查集模板題,當時腦抽,寫了一個while,沒跳出死循環,然后累加數據的時候出錯了,隊友講題的時候,還大言不慚說找不出錯。。。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL long long
#define MAXN 1005
#define CLR(x) memset(x, 0, sizeof(x))

int pre[MAXN], a[MAXN], res[MAXN];

int cmp(int x, int y)
{
    return x > y;
}

int Find(int x)
{
    return pre[x]==x ? pre[x] : Find(pre[x]);
}

void Uion(int x, int y)
{
    int xx = Find(x), yy = Find(y);
    if (xx != yy)
    {
        pre[xx] = yy;
        //a[yy] += a[xx]; 為啥不在這累加,手推一下就知道了
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    int k = 1;
    while(t--)
    {
        CLR(res);
        CLR(a);
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 0; i <= n; i++)
            pre[i] = i;
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        int u, v;
        for (int i = 0; i < m; i++)
        {
            scanf("%d %d", &u, &v);
            Uion(u, v);
        }
        int cnt = 0;
        for (int i = 1; i <= n; i++)
        {
            if (pre[i]==i) cnt++;  //原本寫了一個while。。無明確跳出,死循環,第一題就一直TLE,心態炸啊
            res[Find(i)] += a[i];
        }
        sort(res+1, res+n+1, cmp);
        if (n==1)
        {
            printf("Case %d: 1\n", k++);
            printf("%d\n", a[1]);
            continue;
        }
        printf("Case %d: %d\n", k++, cnt);
        for (int i = cnt; i >= 1; i--)
        {
            if (i == 1) printf("%d\n", res[1]);
            else printf("%d ", res[i]);
        }
    }
    return 0;
}

C - Question 3

SPOJ - VECTAR1
這題是個矩陣異或。。。記住vis矩陣要開對大小,不然一定會wa

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
#define LL long long
#define MAXN 1005
#define mod 1000000007
#define CLR(x) memset(x, 0, sizeof(x))

int vis[1000000]; //筆者直接開了1000^2
LL maxx;

void BuildMart(int n, int m)
{
    CLR(vis);
    //CLR(mat);
    maxx = -1;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            vis[i^j]++;
            maxx = max(maxx, (LL)i^j);
        }
    //return maxx;
}

LL JieCeng(LL n)
{
    LL ans = 1;
    for (int i = 2; i <= n; i++)
    {
        ans *= i;
        ans %= mod;
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, m;
        scanf("%d%d", &n, &m);
        BuildMart(n, m);
        LL res = 1;
        for (int i = 0; i <= maxx; i++)
        {
            if (vis[i])
            {
                res *= JieCeng((LL)vis[i]);
                res %= mod;
            }
        }
        printf("%lld\n", res);
    }
}

D - Question 4

CodeForces - 779B
最簡單的一題,筆者理解錯意思了,WA在test20,8發,心態崩了,后來在code forces上測數據才明白搞錯意思了
筆者 想復雜了,以為要各種分類討論。。。最近腦子抽。。。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL long long
#define MAXN 1005
#define CLR(x) memset(x, 0, sizeof(x))
char str[15];
int x;
int main()
{
    scanf("%s %d", str, &x);
    int len = strlen(str);
    int zero = 0, fz = 0;
    for (int i = len-1; i >= 0; i--)
    {
        if (str[i] == '0') zero++;
        else fz++;
        if (zero == x) break;
    }
    if (zero == x) printf("%d", fz); //如果可以整除,輸出需要去除的數的個數
    else printf("%d", len-1); //否則直接輸出長度減一

    return 0;
}

E - Question 5

CodeForces - 779C
E題其實就是貪心。。。然后,補題的時候理解錯題意,數值一直不對,要不就是wa,后來的紙上推了一遍,發現。。。又想多了。。。或者說忘了自己排過序了。。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define LL long long
#define mod 1000000007
#define CLR(x) memset(x, 0, sizeof(x))
int n ,m ;
struct node{
    int now, next;
    int d;
}prc[200005];

int cmp(node x, node y)
{
    //if (x.d == y.d) return x.now < y.now;
    return x.d > y.d;
}


int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%d", &prc[i].now);
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
         scanf("%d", &prc[i].next);
         prc[i].d = prc[i].next - prc[i].now;
         if (prc[i].d > 0) cnt++; //這周買合算
    }
    sort(prc, prc+n, cmp); //由這周買最合算到最不合算

    LL ans = 0;
    for (int i = 0; i < n; i++)
    {
        if (prc[i].d > 0)//排過序,前頭的肯定是最合算
        {
            ans += prc[i].now;
            m--;
        }
        else//本周買最合算的買完后
        {
            if (m > 0) //本周還需買m-cnt個商品
            {
                ans += prc[i].now;
                m--;
            }
            else //下周買
                ans += prc[i].next;
        }
    }
    printf("%I64d", ans);
}

最近真的太急功近利了,該緩緩,好好補缺補漏,打好基礎才能走的更遠!!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 如果你說太長看不完,就對了,這是一周的讀書內容,每天十分鐘,五天看完很輕松! 目錄 《精要主義》-精讀 Day 1...
    超姐666閱讀 2,109評論 0 13
  • 2046 文/王三歲 “ 那么,再見了。”墨涵輕聲說著,按下了啟動鍵。實驗室里的譚小優隔著頭罩驚恐地看著墨涵,她聽...
    大故事家閱讀 493評論 0 2
  • 你也知道,我以前每天都會看到許許多多的酒客。 他們,有西裝平整而領帶搭肩的;有打扮精致而眼著淚痕的;有衣衫破舊而執...
    豐山心閱讀 485評論 0 1
  • 杜甫先生有一句廣為人知的詩句:“安得廣廈千萬間,大庇天下寒士俱歡顏,風雨不動安如山。”——詩中的廣廈既是他的美好理...
    古董碎片閱讀 450評論 2 6