Codeforces Round #227 (Div. 2)

A. George and Sleep
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t.

Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample).

Input

The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" — the duration of George‘s sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00?≤?hh?≤?2300?≤?mm?≤?59.

Output

In the single line print time p — the time George went to bed in the format similar to the format of the time in the input.

Sample test(s)
input
05:50
05:44
output
00:06
input
00:00
01:00
output
23:00
input
00:01
00:00
output
00:01
Note

In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That‘s why answers "0:06", "00:6" and others will be considered incorrect.

In the second sample, George went to bed yesterday.

In the third sample, George didn‘t do to bed at all.

A:水题,直接模拟时间即可。

#include <stdio.h>
#include <string.h>

int hh, mm;
int hhh, mmm;

int main() {
    scanf("%d%*c%d", &hh, &mm);
    scanf("%d%*c%d", &hhh, &mmm);
    int ansh, ansm;
    if (mm >= mmm) ansm = mm - mmm;
    else {ansm = mm + 60 - mmm; hh--;}
    if (hh >= hhh) ansh = hh - hhh;
    else ansh = hh + 24 - hhh;
    printf("%02d:%02d\n", ansh, ansm);
    return 0;
}

B. George and Round
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

George decided to prepare a Codesecrof round, so he has prepared m problems for the round. Let‘s number the problems with integers1 through m. George estimates the i-th problem‘s complexity by integer bi.

To make the round good, he needs to put at least n problems there. Besides, he needs to have at least one problem with complexity exactly a1, at least one with complexity exactly a2, ..., and at least one with complexity exactly an. Of course, the round can also have problems with other complexities.

George has a poor imagination. It‘s easier for him to make some already prepared problem simpler than to come up with a new one and prepare it. George is magnificent at simplifying problems. He can simplify any already prepared problem with complexity c to any positive integer complexity d (c?≥?d), by changing limits on the input data.

However, nothing is so simple. George understood that even if he simplifies some problems, he can run out of problems for a goodround. That‘s why he decided to find out the minimum number of problems he needs to come up with in addition to the m he‘s prepared in order to make a good round. Note that George can come up with a new problem of any complexity.

Input

The first line contains two integers n and m (1?≤?n,?m?≤?3000) — the minimal number of problems in a good round and the number of problems George‘s prepared. The second line contains space-separated integers a1,?a2,?...,?an (1?≤?a1?<?a2?<?...?<?an?≤?106) — the requirements for the complexity of the problems in a good round. The third line contains space-separated integers b1,?b2,?...,?bm (1?≤?b1?≤?b2...?≤?bm?≤?106) — the complexities of the problems prepared by George.

Output

Print a single integer — the answer to the problem.

Sample test(s)
input
3 5
1 2 3
1 2 2 3 3
output
0
input
3 5
1 2 3
1 1 1 1 1
output
2
input
3 1
2 3 4
1
output
3
Note

In the first sample the set of the prepared problems meets the requirements for a good round.

In the second sample, it is enough to come up with and prepare two problems with complexities 2 and 3 to get a good round.

In the third sample it is very easy to get a good round if come up with and prepare extra problems with complexities: 2,?3,?4.

B题:先两个数组排序,把能用的B用完,看还剩多少个A没用。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1000005;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int n, m, i, j;
int a[3005], b[3005];
int Max = 0;
int main() {
    scanf("%d%d", &n, &m);
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for (i = 0; i < m; i++)
        scanf("%d", &b[i]);
    sort(a, a + n);
    sort(b, b + m);
    int ans = 0;
    int l = 0, r = 0;
    while (l < n && r < m) {
        if (a[l] <= b[r]) {
            l++; r++;
        }
        else {
            r++;
        }
    }
    ans = n - l;
    printf("%d\n", ans);
    return 0;
}

C. George and Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers b. During the game, George modifies the array by using special changes. Let‘s mark George‘s current array as b1,?b2,?...,?b|b| (record |b| denotes the current length of the array). Then one change is a sequence of actions:

  • Choose two distinct indexes i and j (1?≤?i,?j?≤?|b|; i?≠?j), such that bi?≥?bj.
  • Get number v?=?concat(bi,?bj), where concat(x,?y) is a number obtained by adding number y to the end of the decimal record of number x. For example, concat(500,?10)?=?50010concat(2,?2)?=?22.
  • Add number v to the end of the array. The length of the array will increase by one.
  • Remove from the array numbers with indexes i and j. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.

George played for a long time with his array b and received from array b an array consisting of exactly one number p. Now George wants to know: what is the maximum number of elements array b could contain originally? Help him find this number. Note that originally the array could contain only positive integers.

Input

The first line of the input contains a single integer p (1?≤?p?<?10100000). It is guaranteed that number p doesn‘t contain any leading zeroes.

Output

Print an integer — the maximum number of elements array b could contain originally.

Sample test(s)
input
9555
output
4
input
10000000005
output
2
input
800101
output
3
input
45
output
1
input
1000000000000001223300003342220044555
output
17
input
19992000
output
1
input
310200
output
2

C题:贪心,从后面往前推,如果可以分离就直接分离ans++,不行就在继续添加数字。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 100005;

char seq[N];
int i, j;
char save[N];
int sn = 0;
int len;

bool judge(int len) {
    if (i > sn) return true;
    if (i < sn) return false;
    if (i == sn) {
        for (int j = sn - 1; j >= 0; j--) {
            if (seq[sn - 1 - j] < save[j]) return false;
        }
    }
    return true;
}

int main() {
    int ans = 1;
    scanf("%s", seq);
    len = strlen(seq);
    for (i = len - 1; i >= 0; i--) {
        if (seq[i] != ‘0‘) {
            save[sn++] = seq[i];
            if (judge(i)) {
                ans++;
                sn = 0;
            }
        }
        else {
            save[sn++] = seq[i];
        }
    }
    printf("%d\n", ans);
    return 0;
}

D. George and Interesting Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

George loves graphs. Most of all, he loves interesting graphs. We will assume that a directed graph is interesting, if it meets the following criteria:

  • The graph doesn‘t contain any multiple arcs;
  • There is vertex v (we‘ll call her the center), such that for any vertex of graph u, the graph contains arcs (u,?v) and (v,?u). Please note that the graph also contains loop (v,?v).
  • The outdegree of all vertexes except for the center equals two and the indegree of all vertexes except for the center equals two. The outdegree of vertex u is the number of arcs that go out of u, the indegree of vertex u is the number of arcs that go in u. Please note that the graph can contain loops.

However, not everything‘s that simple. George got a directed graph of n vertices and m arcs as a present. The graph didn‘t have any multiple arcs. As George loves interesting graphs, he wants to slightly alter the presented graph and transform it into an interesting one. In one alteration he can either remove an arbitrary existing arc from the graph or add an arbitrary arc to the graph.

George wonders: what is the minimum number of changes that he needs to obtain an interesting graph from the graph he‘s got as a present? Help George and find the answer to the question.

Input

The first line contains two space-separated integers n and m (2?≤?n?≤?500,?1?≤?m?≤?1000) — the number of vertices and arcs in the presented graph.

Each of the next m lines contains two space-separated integers ai,?bi (1?≤?ai,?bi?≤?n) — the descriptions of the graph‘s arcs. Pair(ai,?bi) means that the graph contains an arc from vertex number ai to vertex number bi. It is guaranteed that the presented graph doesn‘t contain multiple arcs.

Assume that the grah vertices are numbered 1 through n.

Output

Print a single integer — the answer to George‘s question.

Sample test(s)
input
3 7
1 1
2 2
3 1
1 3
3 2
2 3
3 3
output
0
input
3 6
1 1
2 2
3 1
3 2
2 3
3 3
output
1
input
3 1
2 2
output
6
D题:二分图匹配,先枚举中心点,然后剩余点去求最大匹配数。就可以找出需要添加和删除的边。

代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define INF 0x3f3f3f3f
const int N = 505;

int n, m, g[N][N], ind[N], out[N], match[N], vis[N];

bool find(int u, int i) {
	for (int j = 1; j <= n; j++) {
		if (u == j || vis[j] || !g[i][j]) continue;
		vis[j] = 1;
		if (match[j] == -1 || find(u, match[j])) {
			match[j] = i;
			return true;
		}
	}
	return false;
}

int cal(int u) {
	int ans = 0;
	memset(match, -1, sizeof(match));
	for (int i = 1; i <= n; i++) {
		if (i == u) continue;
		memset(vis, 0, sizeof(vis));
		if (find(u, i)) ans++;
	}
	return ans;
}

int solve(int u) {
	int uedge = ind[u] + out[u] - g[u][u];//与u点相连的边数
	int ans = 2 * n - 1 - uedge;
	int Max = cal(u);
	ans += m - uedge - Max; //需要删除的边数
	ans += n - 1 - Max; //需要添加的边数
	return ans;
}

int main() {
	scanf("%d%d", &n, &m);
	int x, y, ans = INF, mm = m;
	while (mm--) {
		scanf("%d%d", &x, &y);
		g[x][y] = 1;
		ind[y]++;
		out[x]++;
	}
	for (int i = 1; i <= n; i++) {
		int t = solve(i);
		ans = min(ans, t);
	}
	printf("%d\n", ans);
	return 0;
}

E. George and Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

George is a cat, so he loves playing very much.

Vitaly put n cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let‘s number the cards from the left to the right with integers from 1 to n. Then the i-th card from the left contains number pi (1?≤?pi?≤?n).

Vitaly wants the row to have exactly k cards left. He also wants the i-th card from left to have number bi written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operation n?-?k times.

In one remove operation George can choose w (1?≤?ww is not greater than the current number of cards in the row) contiguous cards (contiguous subsegment of cards). Let‘s denote the numbers written on these card as x1,?x2,?...,?xw (from the left to the right). After that, George can remove the card xi, such that xi?≤?xj for each j (1?≤?j?≤?w). After the described operation George gets w pieces of sausage.

George wondered: what maximum number of pieces of sausage will he get in total if he reaches his goal and acts optimally well? Help George, find an answer to his question!

Input

The first line contains integers n and k (1?≤?k?≤?n?≤?106) — the initial and the final number of cards.

The second line contains n distinct space-separated integers p1,?p2,?...,?pn (1?≤?pi?≤?n) — the initial row of cards.

The third line contains k space-separated integers b1,?b2,?...,?bk — the row of cards that you need to get. It is guaranteed that it‘s possible to obtain the given row by using the remove operation for n?-?k times.

Output

Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.

Sample test(s)
input
3 2
2 1 3
1 3
output
1
input
10 5
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10
output
30

E题:二分。树状数组。先保留数字位置,然后优先从小的开始move,计算区间长度的时候利用树状数组去计算并维护。

代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
const int N = 1000005;
#include <set>
using namespace std;
#define INF 0x3f3f3f3f

int n, k, v[N], vis[N], i, bit[N];

void Insert(int x, int v) {
    while (x <= n) {
        bit[x] += v;
        x += (x&(-x));
    }
}

int Sum(int x) {
    int ans = 0;
    while (x > 0) {
        ans += bit[x];
        x -= (x&(-x));
    }
    return ans;
}

__int64 solve() {
    __int64 ans = 0;
    set<int> s;
    s.insert(0);
    s.insert(n + 1);
    for (int i = 1; i <= n; i++) {
        if (vis[i]) {
            s.insert(v[i]);
        }
        else {
            set<int>::iterator it = s.lower_bound(v[i]);
            int r = *it - 1;
            int l = *(--it);
            ans += Sum(r) - Sum(l);
            Insert(v[i], -1);
        }
    }
    return ans;
}

int main() {
    scanf("%d%d", &n, &k);
    int num;
    for (i = 1; i <= n; i++) {
        scanf("%d", &num);
        v[num] = i;
        Insert(i, 1);
    }
    for (i = 1; i <= k; i++) {
        scanf("%d", &num);
        vis[num] = 1;
    }
    printf("%I64d\n", solve());
    return 0;
}


Codeforces Round #227 (Div. 2)

上一篇:单例模式在Unity中的应用


下一篇:TXT阅读神器 分分钟打造一本电子书