最近腦子抽的厲害贱鄙,東學一點劝贸,西補一點,著急+浮躁+粗心=爆零啊逗宁。
A - Question 1
SPOJ - SERGRID
這就道BFS模板題映九,沒啥好說的,清醒后一發(fā)過瞎颗。件甥。。
#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];/*可以跳躍的步數(shù)*/
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'; //注意數(shù)字是連續(xù)輸入的哼拔,要處理一下
}
}
int res = BFS();
printf("%d", res);
return 0;
}
B - Question 2
SPOJ - IAPCR2F **
這題引有。。并查集模板題倦逐,當時腦抽譬正,寫了一個while,沒跳出死循環(huán)檬姥,然后累加數(shù)據(jù)的時候出錯了曾我,隊友講題的時候,還大言不慚說找不出錯健民。抒巢。。
#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虐秦。。無明確跳出凤优,死循環(huán)悦陋,第一題就一直TLE,心態(tài)炸啊
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發(fā)栖袋,心態(tài)崩了拍顷,后來在code forces上測數(shù)據(jù)才明白搞錯意思了
筆者 想復雜了,以為要各種分類討論塘幅。昔案。。最近腦子抽电媳。踏揣。。
#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); //如果可以整除匾乓,輸出需要去除的數(shù)的個數(shù)
else printf("%d", len-1); //否則直接輸出長度減一
return 0;
}
E - Question 5
CodeForces - 779C
E題其實就是貪心捞稿。。拼缝。然后娱局,補題的時候理解錯題意,數(shù)值一直不對咧七,要不就是wa铃辖,后來的紙上推了一遍,發(fā)現(xiàn)猪叙。娇斩。。又想多了穴翩。犬第。∶⑴粒或者說忘了自己排過序了歉嗓。。
#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);
}
最近真的太急功近利了鉴分,該緩緩,好好補缺補漏带膀,打好基礎才能走的更遠V菊洹!