Codeforces Round #345 (Div 2)

来源:互联网 发布:陕甘回乱 左中堂知乎 编辑:程序博客网 时间:2024/06/06 01:47

最后两题是orzCJK学长帮忙代打的,不过总算是到蓝名了(上次睡迟了,只剩半个小时,结果作大死点开题目看,结果rating掉了100多),还有论代码风格的重要性!!!(没写空格被学长各种D)




A题

题目简意:

有两个人做游戏,每个人有一块电池,给定初始电量a,b,每一秒你可以给一块电池充1%的电,另一块电池就会掉2%的电,当有一个没电时游戏结束。求游戏的最长时间。

input
3 5
output
6
input
4 4
output
5

题解:

大概就是贪心吧,每次给电少的电池充电,注意细节(当有一个电池的电量少于2%时不能充电)。

代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#ifdef WIN32#define LL "%I64d"#else#define LL "%lld"#endif#ifdef CT#define debug(...) printf(__VA_ARGS__)#else#define debug(...)#endif#define R register#define getc() (S==T&&(T=(S=B)+fread(B,1,1<<15,stdin),S==T)?EOF:*S++)#define gmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))#define gmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)char B[1<<15],*S=B,*T=B;inline int FastIn(){R char ch;R int cnt=0;R bool minus=0;while (ch=getc(),(ch < '0' || ch > '9') && ch != '-') ;ch == '-' ?minus=1:cnt=ch-'0';while (ch=getc(),ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';return minus?-cnt:cnt;}int main(){R int a1=FastIn(),a2=FastIn(),ans=0;while ((a1>1||a2>1)&&a1>0&&a2>0){ans++;a1>a2?(a1-=2,++a2):(a2-=2,++a1); }printf("%d\n",ans );return 0;}


B

题目简意:

给定一个序列,你可以任意排列,问排列后前一个数比后一个数大的个数的最大值。

input
520 30 10 50 40
output
4
input
4200 100 100 200
output
2

题解:

我们可以转换一下思路,这题其实可以看做求数字出现次数的最大值,因为如果某个数出现的次数是最多的,那么这些数必然会被分在不同的组里(我们假装每个组都是又偏序关系的)。所以答案就是(N-出现最多的次数)。

代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#ifdef WIN32#define LL "%I64d"#else#define LL "%lld"#endif#ifdef CT#define debug(...) printf(__VA_ARGS__)#else#define debug(...)#endif#define R register#define getc() (S==T&&(T=(S=B)+fread(B,1,1<<15,stdin),S==T)?EOF:*S++)#define gmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))#define gmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)char B[1<<15],*S=B,*T=B;inline int FastIn(){R char ch;R int cnt=0;R bool minus=0;while (ch=getc(),(ch < '0' || ch > '9') && ch != '-') ;ch == '-' ?minus=1:cnt=ch-'0';while (ch=getc(),ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';return minus?-cnt:cnt;}#define maxn 1010int num[maxn];int main(){R int n=FastIn(),maxx=0,x;for (R int i=1;i<=n;i++)num[x=FastIn()]++,cmax(maxx,num[x]);printf("%d\n",n-maxx );return 0;}


C

题目简意:

平面上给定N个点,求欧几里得距离和曼哈顿距离相等的点对个数。

input
31 17 51 5
output
2
input
60 00 10 2-1 10 11 1
output
11

题解:

欧几里得距离和曼哈顿距离相等嘛。。。不就是在同一条平行于x轴或平行于y轴的直线上吗?排个序然后搞一搞就好啦~

代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#ifdef WIN32#define LL "%I64d"#else#define LL "%lld"#endif#ifdef CT#define debug(...) printf(__VA_ARGS__)#else#define debug(...)#endif#define R register#define getc() (S==T&&(T=(S=B)+fread(B,1,1<<15,stdin),S==T)?EOF:*S++)#define gmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))#define gmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)char B[1<<15],*S=B,*T=B;inline int FastIn(){R char ch;R int cnt=0;R bool minus=0;while (ch=getc(),(ch < '0' || ch > '9') && ch != '-') ;ch == '-' ?minus=1:cnt=ch-'0';while (ch=getc(),ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';return minus?-cnt:cnt;}#define maxn 200010struct Poi{int x,y;}p[maxn];inline bool cmp1(const Poi &i,const Poi &j){return i.x<j.x;}inline bool cmp2(const Poi &i,const Poi &j){return i.y<j.y||(i.y==j.y&&i.x<j.x);}int main(){R int n=FastIn();R long long ans=0;for (R int i=1;i<=n;i++){p[i]=(Poi){FastIn(),FastIn()};}std::sort(p+1 ,p+n+1,cmp1);R int cnt=0;for (R int i=1;i<=n;i++){if (i>1&&p[i].x==p[i-1].x)ans+=cnt++;else cnt=1;}std::sort(p+1,p+n+1,cmp2);cnt=0;for (R int i=1;i<=n;i++){if (i>1&&p[i].y==p[i-1].y)ans+=cnt++;else cnt=1;}cnt=0;for (R int i=1;i<=n;i++){if (i>1&&p[i].x==p[i-1].x&&p[i].y==p[i-1].y) ans-=cnt++;else cnt=1;}printf("%lld\n",ans );return 0;}

D

题目简意:

给定一个长度为N的字符串,‘w’代表需要旋转的,‘h’代表不需要旋转的,翻到下一张照片需要a秒,旋转需要b秒,看一张照片需要1s,求T秒内看的照片的最大数量。

input
4 2 3 10wwhw
output
2
input
5 2 4 13hhwhh
output
4
input
5 2 4 1000hhwhh
output
5
input
3 1 100 10whw
output
0
题解:

我们猜一个结论(雾),你枚举一个左端点,它右端点的变化是不降的。然后这个结论也很好脑补。然后就做完啦。。。

代码:

#include <bits/stdc++.h>using namespace std;#define RG register#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)#define maxn 1000010char str[maxn];int sum[maxn];int n, a, b;inline int cost(RG int l, RG int r){RG int ans = sum[r] - sum[l - 1];RG int L = (n + 1) - l;RG int R = r - (n + 1);RG int g = ans + a * dmin(L + L + R, L + R + R);return g;}int main(){RG int T;cin >> n >> a >> b >> T >> (str + 1);for(RG int i = 1; i <= n; ++i) str[i + n] = str[i];for(RG int i = 1; i <= (n << 1); ++i) sum[i] = sum[i - 1] + (str[i] == 'w' ? b + 1 : 1);RG int ans = 0;RG int l = 2;for(RG int r = n + 1; r <= (n << 1); ++r){cmax(l, r - n + 1);while(l <= n + 1 && cost(l, r) > T) ++l;if(l > n + 1) break;cmax(ans, r - l + 1);}cout << ans << endl;}


E

题目简意:

给定一个N*M的矩阵,让你给出一种方案,使得原矩阵每一行和每一列的偏序关系不变,然后新矩阵的最大的数最小。

input
2 21 23 4
output
1 22 3
input
4 320 10 3050 40 3050 60 7090 80 70
output
2 1 35 4 35 6 79 8 7
题解:

并查集+最长路。先用并查集把每一行/每一列的相同的数并起来,然后再建图,每个点只会向相邻的点连边,求这个点的最长路。

代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#ifdef WIN32#define LL "%I64d"#else#define LL "%lld"#endif#ifdef CT#define debug(...) printf(__VA_ARGS__)#else#define debug(...)#endif#define R register#define getc() (S==T&&(T=(S=B)+fread(B,1,1<<15,stdin),S==T)?EOF:*S++)#define gmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))#define gmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)char B[1<<15],*S=B,*T=B;inline int FastIn(){R char ch;R int cnt=0;R bool minus=0;while (ch=getc(),(ch < '0' || ch > '9') && ch != '-') ;ch == '-' ?minus=1:cnt=ch-'0';while (ch=getc(),ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';return minus?-cnt:cnt;}#define maxn 1000010 #define maxe 2000010#define pos(_i,_j) (((_i)-1)*m+(_j))int last[maxn] , to[maxe] , next[maxe] , w[maxe] , val[maxn] , buff[maxn] , ecnt , dis[maxn] , Fa[maxn];inline bool cmp(const int &i,const int &j) {return val[i] < val[j];}inline int Find(R int x){return Fa[x]==x ? x : Fa[x] = Find(Fa[x]);}#define add(_a,_b,_v) ( to[++ecnt] = (_b) , next[ecnt] = last[_a] , last[_a] = ecnt , w[ecnt] = (_v))int dfs(R int x){if (dis[x]) return dis[x];R int tmp=1;for (R int i=last[x];i;i=next[i])cmax(tmp,dfs(Find(to[i]))+w[i]);return dis[x] = tmp;}int main(){R int n = FastIn() , m = FastIn();for (R int i = 1 ; i<=n ; i++)for (R int j = 1 ; j<=m ; j++)val[pos(i,j)] = FastIn() , Fa[pos(i,j)] = pos(i,j);for (R int i = 1 ; i <= n ; i++){for (R int j = 1 ; j<=m ; j++) buff[j] = pos(i,j);std::sort ( buff+1 , buff+m+1 , cmp);for (R int j=1;j<m;j++){val[buff[j]]==val[buff[j+1]] ? Fa[Find(buff[j])] = Find (buff[j+1]) : 0;}}for (R int j = 1 ; j<=m ; j++){for (R int i = 1 ; i<=n ; i++) buff[i] = pos(i,j);std::sort(buff+1 , buff+n+1 , cmp);for (R int i = 1 ; i<n ; i++){val[buff[i]]==val[buff[i+1]] ? Fa[Find(buff[i])] = Find (buff[i+1]) : 0;}}for (R int i = 1 ; i<=n ; i++){for (R int j = 1 ; j<=m ; j++) buff[j] = pos(i,j);std::sort(buff+1 , buff+m+1 , cmp);for (R int j = 1 ; j<m ; j++) {val[buff[j]]!=val[buff[j+1]] ? add(Find(buff[j+1]),Find(buff[j]),1) : 0;}}for (R int j = 1 ; j<=m ; j++){for (R int i = 1; i<=n ; i++) buff[i] = pos(i,j);std::sort(buff+1 , buff+n+1 , cmp);for (R int i = 1 ; i<n ; i++){val[buff[i]]!=val[buff[i+1]] ? add(Find(buff[i+1]),Find(buff[i]),1) : 0;}}for (R int i=1;i<=n*m;i++)if (!dis[Find(i)])dfs(Find(i));for (R int i=1;i<=n;i++){for (R int j=1;j<=m;j++)printf("%d ",dis[Find(pos(i,j))]);puts("");}return 0;}



0 0
原创粉丝点击