您的位置:首页 > 其它

Codeforces Round #352(Div 2)

2016-05-12 13:50 281 查看
又一次半夜打CF。(队友太强直接秒题解,然而我座位比较远耳朵不好(雾)听不见)总之,最后结果400多名。估计还能重新上蓝。

A题:

*题目描述:

请你求这样的序列“1234567891011……”的第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__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif

#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#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)
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 a[2010], stack[1000];
int main()
{
//  setfile();
R int n = FastIn(), tot = 0;
for (R int i = 1; tot <= 1000; ++i)
{
R int tmp = i, top = 0;
while (tmp)
{
stack[++top] = tmp % 10;
tmp /= 10;
}
for (R int i = top; i; --i)
a[++tot] = stack[i];
}
printf("%d\n",a
);
return 0;
}


B题:

*题目描述:

给定一个字符串,求最少需要修改多少位使得每一位都不同。

*题解:

统计一下每种字母出现了多少次。然后用长度减一下。

*代码:

#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__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif

#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#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)
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 100010
int cnt[26];
char str[maxn];
int main()
{
//  setfile();
R int n, ans = 0;
scanf("%d\n", &n);
gets(str);
for (R int i = 0; str[i]; ++i) ++cnt[str[i] - 'a'];
for (R int i = 0; i < 26; ++i) if (cnt[i]) ans++;
if (n <= 26) printf("%d\n",n - ans );
else puts("-1");
return 0;
}


C题:

*题目描述:

二维平面上有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__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif

#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#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)
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 100010
struct Poi
{
double x, y;
}p[maxn], a, b, t;
#define dist(_i, _j) sqrt((_i.x - _j.x) * (_i.x - _j.x) + (_i.y - _j.y) * (_i.y - _j.y))
bool vis[maxn];
struct Dist
{
double d;
int r;
inline bool operator < (const Dist &that) const {return d < that.d; }
}d1[maxn], d2[maxn];
double d3[maxn];
#define inf 1e18
int main()
{
//  setfile();
a = (Poi) {FastIn(), FastIn()};
b = (Poi) {FastIn(), FastIn()};
t = (Poi) {FastIn(), FastIn()};
R double ans = 0;
R int n = FastIn();
for (R int i = 1; i <= n; ++i) p[i] = (Poi) {FastIn(), FastIn()};
for (R int i = 1; i <= n; ++i)
{
d3[i] = dist(p[i], t);
d1[i].d = dist(p[i], a) - d3[i];
d2[i].d = dist(p[i], b) - d3[i];
ans += d3[i] * 2;
d1[i].r = i;
d2[i].r = i;
}
std::sort(d1 + 1, d1 + n + 1);
std::sort(d2 + 1, d2 + n + 1);
if (d1[1].d < 0 && d2[1].d < 0)
{
R double tmp = inf;
if (d1[1].r != d2[1].r) ans += d1[1].d + d2[1].d;
else
{
cmin(tmp, d2[1].d);
cmin(tmp, d2[1].d + d1[2].d);
cmin(tmp, d1[1].d);
cmin(tmp, d1[1].d + d2[2].d);
ans += tmp;
}
}
else
ans += dmin(d1[1].d, d2[1].d);
printf("%.10lf\n",ans );
return 0;
}


D题:

*题目描述:

有n个人,每个人的资产为ai,每一天鲁滨孙会让最富的那个人给最穷的人一块钱,问k天以后最富的人和最穷的人的差距是多少。

*题解:

第一反应是堆强行模拟,然而写完交上去TLE了,然后才看见k<=10^9这个条件,伤心。。。队长Lightning巨强,当场就a了,我比赛后想了一种二分最大值和最小值的方法,然后队长说他的复杂度是线性的(我感觉过去细节巨多)。。。

我们发现答案实际上是最小的最大值减去最大的最小值,于是乎我果断想到两次二分。求出割掉至多k个的最小的最大值和至多补上k个的最大的最小值。

*代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif

#ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif

#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#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)
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 500010
const int oo = 1e9;
int v[maxn], n, k;
inline bool check(R int x, R int opt)
{
R long long tmp = 0;
if (!opt)
{
for (R int i = n; i && v[i] > x; --i)
tmp += v[i] - x;
return tmp <= k;
}
else
{
for (R int i = 1; i <= n && v[i] < x; ++i)
tmp += x - v[i];
return tmp <= k;
}
}
int main()
{
//  setfile();
n = FastIn(), k = FastIn();
R long long sum = 0;
for (R int i = 1; i <= n; ++i)
v[i] = FastIn(), sum += v[i];
std::sort(v + 1, v + n + 1);
if (v[1] == v
) return !printf("0\n");
R int l = v[1], r = v
, maxx, minn;
while (l < r)
{
R int mid = l + r >> 1;
if (check(mid, 0)) r = mid;
else l = mid + 1;
}
maxx = l;
l = v[1]; r = v
;
while (l < r)
{
R int mid = l + r + 1 >> 1;
if (check(mid, 1)) l = mid;
else r = mid - 1;
}
minn = l;
if (maxx <= minn)
if (sum % n) puts("1");
else puts("0");
else printf("%d\n",maxx - minn );
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces