最后两题是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
5
20 30 10 50 40
output
4
input
4
200 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 1010
int 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
3
1 1
7 5
1 5
output
2
input
6
0 0
0 1
0 2
-1 1
0 1
1 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 200010
struct 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 10
wwhw
output
2
input
5 2 4 13
hhwhh
output
4
input
5 2 4 1000
hhwhh
output
5
input
3 1 100 10
whw
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 1000010
char 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 2
1 2
3 4
output
1 2
2 3
input
4 3
20 10 30
50 40 30
50 60 70
90 80 70
output
2 1 3
5 4 3
5 6 7
9 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;
}

Codeforces Round #345 (Div 2)的更多相关文章

  1. Codeforces Round #581(Div. 2)

    Codeforces Round #581(Div. 2) CF 1204 A. BowWow and the Timetable 题解:发现,$4$的幂次的二进制就是一个$1$后面跟偶数个$0$. ...

  2. Codeforces Round #334(div.2)(新增不用二分代码) B

    B. More Cowbell time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. Codeforces Round #334(div.2) A

    A. Uncowed Forces time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. codeforces Round #389(Div.2)C Santa Claus and Robot(思维题)

    题目链接:http://codeforces.com/contest/752/problem/C 题意:给出一系列机器人的行动方向(机器人会走任意一条最短路径),问最少标记几个点能让机器人按这个 路径 ...

  5. Codeforces Round #626 (Div. 2) B. Count Subrectangles

    题目连接:https://codeforces.com/contest/1323/problem/B 题意:给一个大小为n的a数组,一个大小为m的b数组,c数组是二维数组c[i][j]=a[i]*b[ ...

  6. Codeforces Round #342 (Div 2) 解题报告

    除夕夜之有生之年CF第一场 下午从奶奶家回到姥姥家,一看还有些时间,先吃点水果陪姥姥姥爷聊了会儿,再一看表,5:20....woc已经开场20分钟了...于是抓紧时间乱搞.. **A. Guest F ...

  7. Codeforces Round 1153(div. 2)

    这场奇差.ABCD四题.179名. 但是E在现场有213个人做出. 描述一下我在35分钟做完D后的心路历程. 首先看到这道E,第一下想到的是把所有的横向和竖向的整列(行)求出相连的个数. 然后想如何能 ...

  8. Codeforces Round #622(Div 2)C2. Skyscrapers (hard version)

    题目链接 : C2. Skyscrapers (hard version) 题目描述 : 与上一道题类似,只是数据范围变大, 5e5, 如果用我们原来的方法,铁定是超时的. 考察点 : 单调栈,贪心, ...

  9. Codeforces Round #556(Div.1)

    A 容易发现i,i+1至少有一个数出现,于是可以让尽量多的2和奇数出现 #include<bits/stdc++.h> using namespace std; int n,s1,s2; ...

随机推荐

  1. <<C++ Primer>> 术语表 (总) (待补充)

    术语表 目录 第 1 章 开始 第 I 部分 C++基础 第 2 章 变量和基本类型 第 3 章 字符串, 向量和数组 第 4 章 表达式 第 5 章 语句 第 6 章 函数 第 7 章 类 第 II ...

  2. msql 事务

    START TRANSACTION      delete from t_emp    delete from t_deptcommit START TRANSACTION delete from t ...

  3. SCUT - 492 - 鬼符「搦手的鬼畜生」 - 简单数学

    https://scut.online/p/492 求[1,a]范围内的a模m的逆元的数量. 一开始用扩展欧几里得算法草了一发,WA了,当时不太清楚模非质数的周期,看来扩展欧几里得算法的笔记才知道要加 ...

  4. 在 Chrome DevTools 中调试 JavaScript 入门

    第 1 步:重现错误 找到一系列可一致重现错误的操作始终是调试的第一步. 点击 Open Demo. 演示页面随即在新标签中打开. OPEN DEMO 在 Number 1 文本框中输入 5. 在 N ...

  5. python3小demo

    总结常用的功能小实例,快速学习并掌握python技能 1.墨迹天气 import requests from lxml.html import etree import json import tim ...

  6. vue 中 @click.native.prevent 事件

    在项目中看到@click.native.prevent, 查了一点资料 总结一下, 1.给vue组件绑定事件时候,必须加上native ,否则会认为监听的是来自Item组件自定义的事件, 2.prev ...

  7. es6 filter() 数组过滤方法总结(转载)

    1.创建一个数组,判断数组中是否存在某个值 var newarr = [ { num: 1, val: 'ceshi', flag: 'aa' }, { num: 2, val: 'ceshi2', ...

  8. 日语能力考试N2级必备外来语

    日语能力考试N2级必备外来语 ア行外来语アンテナ:(antenna)         天线インタビュー :(interview)         采访,访谈ウイルス:(virus )        病 ...

  9. js 向数组对象中添加属性和属性值

    let resultList = [{"name":"a1"},{"name":"b1"}] resultList.fo ...

  10. 初探css -11 Table表格

    CSS 表格 使用 CSS 可以使 HTML 表格更美观. Company Contact Country Alfreds Futterkiste Maria Anders Germany Bergl ...