比赛题目(传送门)
A. Juicer
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Kolya is going to make fresh orange juice. He has n oranges of sizes a 1 , a 2 , …, a n . Kolya will put them in the juicer in the fixed order, starting with orange of size a 1 , then orange of size a 2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.
The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d . When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
Input
The first line of the input contains three integers
The second line contains
n
integers
Output
Print one integer — the number of times Kolya will have to empty the waste section.
Examples
input
2 7 10
5 6
output
1
input
1 5 10
7
output
0
input
3 10 10
5 7 7
output
1
input
1 1 1
1
output
0
Note
In the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.
In the second sample, the orange won’t fit in the juicer so Kolya will have no juice at all.
题意
给你
n
个橙子,要选小于等于
解题思路:模拟
创建变量
代码
时间复杂度:
/*头文件模板*/
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)
typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;
template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
int d = p < q ? 1 : -1;
while (p != q) {
if (flag) cout << Gap[0] << *p << Gap[1];
else cout << *p;
p += d;
if (p != q && !flag) cout << Gap;
}
cout << endl;
}
template<typename T>
void print (const T &a, string bes = "") {
int len = bes.length();
if (len >= 2) cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
}
template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
#ifndef ONLINE_JUDGE
int d = p < q ? 1 : -1;
cout << "Debug out : ";
while (p != q) {
if (flag) cout << Gap[0] << *p << Gap[1];
else cout << *p;
p += d;
if (p != q && !flag) cout << Gap;
}
cout << endl;
#endif
}
template<typename T>
void debug (const T &a, string bes = "") {
#ifndef ONLINE_JUDGE
int len = bes.length();
cout << "Debug out : ";
if (len >= 2) cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
#endif
}
void IO_Init() {
ios::sync_with_stdio (false);
}
LL LLabs (const LL a) {
return a >= 0 ? a : -a;
}
const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 1e5 + 5;
const int INF = 0x3f3f3f3f;
/*头文件模板*/
LL n, b, d;
int main() {
#ifndef ONLINE_JUDGE
//FIN;
//FOUT;
#endif
IO_Init();
while(~scanf("%lld%lld%lld", &n, &b, &d)){
LL x, sum = 0, num = 0;
for(int i = 0;i < n;i ++){
scanf("%lld", &x);
if(x > b) continue;
sum += x;
if(sum > d) sum = 0, num ++;
}
printf("%lld\n", num);
}
return 0;
}
B. Checkpoints
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya takes part in the orienteering competition. There are
n
checkpoints located along the line at coordinates
Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.
Input
The first line of the input contains two integers
n
and
The second line contains
n
integers
Output
Print one integer — the minimum distance Vasya has to travel in order to visit at least n − 1 checkpoint.
Examples
input
3 10
1 7 12
output
7
input
2 0
11 -10
output
10
input
5 0
0 0 1000 0 0
output
0
Note
In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 − 10 = 2 ) and then proceed to the second one (distance is 12 − 7 = 5 ). The total distance is equal to 2 + 5 = 7 .
In the second sample it’s enough to visit only one checkpoint so Vasya should just walk to the point − 10 .
题意
给你
n
个点,要经过
解题思路:模拟
有题目可以知道,总共点就只有
n
个,而我要走遍
如此,就只存在四种情况:
从当前位置到达最右边的点 x n ,然后回到第二个点的位置 x 2 ,经历了 n−1 个点
从当前位置到达最右边倒数第二个点 x n−1 ,然后回到第一个点的位置 x 1 ,经历了 n−1 个点
从当前位置到达最左边的点 x 1 ,然后回到最右边倒数第二个点的位置 x n−1 ,经历了 n−1 个点
从当前位置到达最左边的点 x 2 ,然后回到最右边倒数第二个点的位置 x n ,经历了 n−1 个点
取这四种情况的最小值即是答案. [ 注意大数,同时要注意最开始的点可能在所有点的右边,也可能在所有点的左边,所以有些部分要取绝对值 ]
代码
时间复杂度: O(n)
/*头文件模板*/
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)
typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;
template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
int d = p < q ? 1 : -1;
while (p != q) {
if (flag) cout << Gap[0] << *p << Gap[1];
else cout << *p;
p += d;
if (p != q && !flag) cout << Gap;
}
cout << endl;
}
template<typename T>
void print (const T &a, string bes = "") {
int len = bes.length();
if (len >= 2) cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
}
template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
#ifndef ONLINE_JUDGE
int d = p < q ? 1 : -1;
cout << "Debug out : ";
while (p != q) {
if (flag) cout << Gap[0] << *p << Gap[1];
else cout << *p;
p += d;
if (p != q && !flag) cout << Gap;
}
cout << endl;
#endif
}
template<typename T>
void debug (const T &a, string bes = "") {
#ifndef ONLINE_JUDGE
int len = bes.length();
cout << "Debug out : ";
if (len >= 2) cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
#endif
}
void IO_Init() {
ios::sync_with_stdio (false);
}
LL LLabs (const LL a) {
return a >= 0 ? a : -a;
}
const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 1e5 + 5;
const int INF = 0x3f3f3f3f;
/*头文件模板*/
LL n, a;
LL A[MAXN];
int main() {
#ifndef ONLINE_JUDGE
//FIN;
//FOUT;
#endif
IO_Init();
while(~scanf("%lld%lld", &n, &a)) {
for(int i = 0; i < n; i ++) {
scanf("%lld", &A[i]);
}
if(n == 1){
puts("0");
continue;
}
sort(A, A + n);
LL a1 = abs(A[n - 1] - a) + A[n - 1] - A[1];
LL a2 = abs(A[n - 2] - a) + A[n - 2] - A[0];
LL a3 = abs(a - A[1]) + A[n - 1] - A[1];
LL a4 = abs(a - A[0]) + A[n - 2] - A[0];
printf("%lld\n", min(a1, min(a2, min(a3, a4))));
}
return 0;
}
C. Letters Cyclic Shift
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a non-empty string
s
consisting of lowercase English letters. You have to pick exactly one non-empty substring of
What is the lexicographically minimum string that can be obtained from s by performing this shift exactly once?
Input
The only line of the input contains the string s(1 ≤ |s| ≤ 100 000) consisting of lowercase English letters.
Output
Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.
Examples
input
codeforces
output
bncdenqbdr
input
abacaba
output
aaacaba
Note
String
题意
给定一个字符串
s
,让你进行有且仅有一次的改变:将这个字符串中的某个子串的每一个字符向前更变即’
解题思路:模拟
分两种情况:
如果不是全部是 a ,就找到第一个不是
a 的子串 sub ,将它全部变化如果全部是 a ,最后一个
a 就变成 z
代码
时间复杂度:
/*头文件模板*/
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)
typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;
template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
int d = p < q ? 1 : -1;
while (p != q) {
if (flag) cout << Gap[0] << *p << Gap[1];
else cout << *p;
p += d;
if (p != q && !flag) cout << Gap;
}
cout << endl;
}
template<typename T>
void print (const T &a, string bes = "") {
int len = bes.length();
if (len >= 2) cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
}
template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
#ifndef ONLINE_JUDGE
int d = p < q ? 1 : -1;
cout << "Debug out : ";
while (p != q) {
if (flag) cout << Gap[0] << *p << Gap[1];
else cout << *p;
p += d;
if (p != q && !flag) cout << Gap;
}
cout << endl;
#endif
}
template<typename T>
void debug (const T &a, string bes = "") {
#ifndef ONLINE_JUDGE
int len = bes.length();
cout << "Debug out : ";
if (len >= 2) cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
#endif
}
void IO_Init() {
ios::sync_with_stdio (false);
}
LL LLabs (const LL a) {
return a >= 0 ? a : -a;
}
const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 1e5 + 5;
const int INF = 0x3f3f3f3f;
/*头文件模板*/
char A[MAXN];
int main() {
#ifndef ONLINE_JUDGE
//FIN;
//FOUT;
#endif
IO_Init();
while(~scanf("%s", A)) {
int l = -1, r = -1, len = strlen(A);
for(int i = 0; i < len; i ++) {
if(A[i] != 'a' && l == -1) {
l = i;
continue;
}
if(l != -1 && A[i] == 'a') {
r = i;
break;
}
}
if(r == -1) r = len;
if(l == -1) l = len - 1;
for(int i = 0; i < len; i ++) {
if(i >= l && i < r) {
char a = A[i] - 1;
if(a < 'a') a = 'z';
printf("%c", a);
} else printf("%c", A[i]);
}
printf("\n");
}
return 0;
}
D. Recover the String
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
For each string
s
consisting of characters ‘
In these problem you are given four integers
a 00
,
a 01
,
a 10
,
a 11
and have to find any non-empty string
s
that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than
Input
The only line of the input contains four non-negative integers a 00 , a 01 , a 10 and a 11 . Each of them doesn’t exceed 10 9 .
Output
If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print “ Impossible ”. The length of your answer must not exceed 1 000 000 .
Examples
input
1 2 3 4
output
Impossible
input
1 2 2 1
output
0110
题意
给你 00,01,10,11 这四个序列每一个的个数。然后要你构造出一个合法的 01 序列。不存在合法的 01 序列就输出 Impossible 。
解题思路:模拟
通过 00 和 11 的个数,我们可以得到他们和 01 , 10 个数关系:
(1 的个数 )×(0 的个数 )=(10 的个数 )+ (01 的个数 )
而求解前两者的方法
我们设
n
和
如果不满足上述条件则直接输出 Impossible .其它情况证明存在一个序列满足条件.
接下来就是构造字符串的方法:
针对当前位置,如果放
0
的话,将增加
代码
时间复杂度:
O(K)
[
/*头文件模板*/
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>
#include <windows.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)
typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;
template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
int d = p < q ? 1 : -1;
while (p != q) {
if (flag) cout << Gap[0] << *p << Gap[1];
else cout << *p;
p += d;
if (p != q && !flag) cout << Gap;
}
cout << endl;
}
template<typename T>
void print (const T &a, string bes = "") {
int len = bes.length();
if (len >= 2) cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
}
template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
#ifndef ONLINE_JUDGE
int d = p < q ? 1 : -1;
cout << "Debug out : ";
while (p != q) {
if (flag) cout << Gap[0] << *p << Gap[1];
else cout << *p;
p += d;
if (p != q && !flag) cout << Gap;
}
cout << endl;
#endif
}
template<typename T>
void debug (const T &a, string bes = "") {
#ifndef ONLINE_JUDGE
int len = bes.length();
cout << "Debug out : ";
if (len >= 2) cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
#endif
}
void IO_Init() {
ios::sync_with_stdio (false);
}
LL LLabs (const LL a) {
return a >= 0 ? a : -a;
}
const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 1e3 + 5;
const int INF = 0x3f3f3f3f;
/*头文件模板*/
int main() {
#ifndef ONLINE_JUDGE
//FIN;
//FOUT;
#endif
IO_Init();
LL x, y, z, k;
while(~scanf("%lld%lld%lld%lld", &x, &y, &z, &k)) {
LL xx = (LL)sqrt(2LL * x) + 1;\\等加数列求和
LL kk = (LL)sqrt(2LL * k) + 1;
if(y == 0 && z == 0) {
if(x == 0) xx = 0;
else kk = 0;
}
if(xx * kk != y + z || xx * (xx - 1) != x * 2 || kk * (kk - 1) != k * 2) {
puts("Impossible");
continue;
}
while(xx + kk){
if(y >= kk){
printf("0");
y -= kk;
xx --;
}
else{
printf("1");
z -= xx;
kk --;
}
}
printf("\n");
}
return 0;
}
E. Centroids(树形DP)
time limit per test4 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Tree is a connected acyclic graph. Suppose you are given a tree consisting of n vertices. The vertex of this tree is called centroid if the size of each connected component that appears if this vertex is removed from the tree doesn’t exceed .
You are given a tree of size n and can perform no more than one edge replacement. Edge replacement is the operation of removing one edge from the tree (without deleting incident vertices) and inserting one new edge (without adding new vertices) in such a way that the graph remains a tree. For each vertex you have to determine if it’s possible to make it centroid by performing no more than one edge replacement.
Input
The first line of the input contains an integer
Output
Print n integers. The
i
-th of them should be equal to
Examples
input
3
1 2
2 3
output
1 1 1
input
5
1 2
1 3
1 4
1 5
output
1 0 0 0 0
Note
In the first sample each vertex can be made a centroid. For example, in order to turn vertex
1
to centroid one have to replace the edge
题意
给出一棵树,要求你最多改变一条边,看这个点能否成为重心
解题思路:树形DP
基本解题思维:
对于一个不是树的重心的节点
v
,在
此时重点即如何进行操作和判断?
设定
son[u]
为当前节点
u
的子树节点个数包括
接下来分两步处理:
先DFS得出每一个节点子树的节点个数和当前向下子树中节点数目小于 n2 的个数的最大值
判断当前点之前和当前之后是否满足条件
[ 由于时间问题,此题题解尚未完善,其中一些原理需要用图片说明,后续补上! ]
代码
时间复杂度:
/*头文件模板*/
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)
typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;
template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
int d = p < q ? 1 : -1;
while (p != q) {
if (flag) cout << Gap[0] << *p << Gap[1];
else cout << *p;
p += d;
if (p != q && !flag) cout << Gap;
}
cout << endl;
}
template<typename T>
void print (const T &a, string bes = "") {
int len = bes.length();
if (len >= 2) cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
}
template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
#ifndef ONLINE_JUDGE
int d = p < q ? 1 : -1;
cout << "Debug out : ";
while (p != q) {
if (flag) cout << Gap[0] << *p << Gap[1];
else cout << *p;
p += d;
if (p != q && !flag) cout << Gap;
}
cout << endl;
#endif
}
template<typename T>
void debug (const T &a, string bes = "") {
#ifndef ONLINE_JUDGE
int len = bes.length();
cout << "Debug out : ";
if (len >= 2) cout << bes[0] << a << bes[1] << endl;
else cout << a << endl;
#endif
}
void IO_Init() {
ios::sync_with_stdio (false);
}
LL LLabs (const LL a) {
return a >= 0 ? a : -a;
}
const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 4e5 + 5;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
/*头文件模板*/
int n, x, y;
int Head[MAXN], tot, son[MAXN];
int pson[MAXN], ans[MAXN];
struct o {
int to, cost, nxt;
} E[MAXN << 1];
void init_edge() {
mem(Head, -1);
tot = 0;
}
void add_edge(int u, int v, int cost) {
E[tot].to = v;
E[tot].cost = cost;
E[tot].nxt = Head[u];
Head[u] = tot ++;
}
void dfs_v(int u, int f) {
ans[u] = 1;
son[u] = 1;
pson[u] = 0;
for(int v = Head[u]; ~v; v = E[v].nxt) {
o &e = E[v];
if(e.to == f) continue;
dfs_v(e.to, u);
son[u] += son[e.to];
pson[u] = max(pson[u], pson[e.to]);
}
if(son[u] <= n >> 1) pson[u] = max(pson[u], son[u]);
}
void dfs_b(int u, int f, int val) {
vector<PII> P;
for(int v = Head[u]; ~v; v = E[v].nxt) {
o &e = E[v];
if(e.to == f) continue;
P.pb(PII(pson[e.to], e.to));
}
sort(P.begin(), P.end(), greater<PII>() );
for(int v = Head[u]; ~v; v = E[v].nxt) {
o &e = E[v];
if(e.to == f) continue;
int nx_val = val;
if(n - son[e.to] <= n >> 1) nx_val = max(nx_val, n - son[e.to]);
if(e.to == P[0].second) {
if(P.size() > 1) {
nx_val = max(nx_val, P[1].first);
}
} else {
nx_val = max(nx_val, P[0].first);
}
if(son[e.to] - pson[e.to] > n >> 1) {
ans[u] = 0;
}
dfs_b(e.to, u, nx_val);
}
if(n - son[u] - val > n >> 1) {
ans[u] = 0;
}
}
int main() {
#ifndef ONLINE_JUDGE
// FIN;
// FOUT;
#endif
IO_Init();
while(~scanf("%d", &n)) {
init_edge();
for(int i = 1; i < n; i ++) {
scanf("%d%d", &x, &y);
add_edge(x, y, 1);
add_edge(y, x, 1);
}
dfs_v(1, 0);//得到数据
dfs_b(1, 0, 0);//判断结果
for(int i = 1; i <= n; i ++) {
printf("%d%c", ans[i], i == n ? '\n' : ' ');
}
}
return 0;
}
[ 有什么问题,记得留言哦!! ]