B

Although Haneen was able to solve the LCS problem, Dr. Ibrahim is suspicious about whether she really understands the LCS problem or not. He believes that she can't write the code on her own, but can only translate the LCS pseudo-code given in class into C++ code without really understanding how it works. Here is the pseudo-code Dr. Ibrahim gave in class:

function LCS (A[1..R], B[1..C])
DP = array(0..R, 0..C)
for i := 0..R
DP[i,0] = 0
for j := 0..C
DP[0,j] = 0
for i := 1..R
for j := 1..C
if A[i] = B[j]
DP[i,j] := DP[i-1,j-1] + 1
else
DP[i,j] := max(DP[i,j-1], DP[i-1,j])
return DP[R,C]

To verify that Haneen understands the LCS problem, Dr. Ibrahim asked her to solve the following problem:

After running the above LCS code on two strings A and B, the 2D array DP is filled with values. Given the 2D array DP, can you guess what A and B are? Any two strings A and B that will produce the given table and contain only lowercase English letters are acceptable.

Can you help Haneen solve this problem?

Input

The first line of input contains two integers R and C (1 ≤ R, C ≤ 25), the length of the strings A and B, respectively.

Each of the following R + 1 lines contains C + 1 integers, these lines represent the 2D array DP.

It's guaranteed that the given table was produced by running the algorithm on two strings that contain only lowercase English letters.

Output

Print string A on the first line and string B on the second line. Both strings should contain only lowercase English letters.

Example

Input
3 4
0 0 0 0 0
0 0 1 1 1
0 0 1 1 2
0 1 1 1 2
Output
abc
cadb
把所有联通的点公用一个字母,注意多对多的字母相等关系。
长度为25,即a-x,所以其他不联通的点都为z
 #include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <stack>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <list>
#include <bitset> using namespace std; typedef long long ll;
const int MAXN = ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ; int n, m, dp[][];
char a[], b[];
int main()
{
cin >> n >> m;
int i, j, k;
for(i = ;i <= n;++i)
for(j = ;j <= m;++j)
cin >> dp[i][j];
for(i = ;i < ;++i)
a[i] = 'a' - + i;
for(i = ;i <= n;++i)
for(j = ;j <= m;++j)
{
if(dp[i][j] != max(dp[i - ][j], dp[i][j - ]))
{
if(b[j])
{
for(k = ;k <= m;++k)
if(b[k] == a[i])
b[k] = b[j];
//把之前与 a[i] 相同的字母都换,所以参考字母 a[i] 不能换
for(k = ;k <= n;++k)
if(a[i] == a[k] && k != i)
a[k] = b[j];
a[i] = b[j];
}
else
b[j] = a[i];
}
} for(i = ;i <= m;++i)
if(!b[i])
b[i] = 'z'; a[n + ] = b[m + ] = ;
cout << (a + ) << endl << (b + ) << endl;
return ;
}

------------------------------------------

D

In this cafeteria, the N tables are all ordered in one line, where table number 1 is the closest to the window and table number N is the closest to the door.

Each time a group of X people enter the cafeteria, one of the cafeteria staff escorts the guests to the table with the smallest number of chairs greater than or equal to X chairs among all available tables. If there’s more than one such table, the guests are escorted to the table that is closest to the window. If there isn't any suitable table available, the group will leave the cafeteria immediately. A table is considered available if no one sits around it.

Eyad likes to help others and also likes to prove that he has learned something from the training of Master Hasan. Therefore, he decided to write a program that helps the cafeteria staff choose the right table for a newly arriving group.

Given the log file of who entered and who left the cafeteria, find the table at which a given group should sit.

Input

The first line of input contains two integers N and Q (1 ≤ N, Q ≤ 105), the number of tables in the cafeteria and the number of events in the log file.

The second line contains N integers, each represents the size of a table (number of chairs around it). The tables are given in order from 1 to N. The size of each table is at least 1 and at most 105.

Each of the following Q lines describes an event in one of the following formats:

- in X: means a group of X (1 ≤ X ≤ 105) people entered the cafeteria.

- out T: means the group on table number T (1 ≤ T ≤ N) just left the cafeteria, the table is now available. It is guaranteed that a group was sitting on this table (input is valid).

Initially, all tables are empty, and the log lists the events in the same order they occurred (in chronological order).

Output

For each event of the first type, print the number of the table on which the coming group should sit. If for any event a group cannot be escorted to any table, print  - 1 for that event.

Example

Input
4 7
1 2 6 7
in 4
in 1
in 3
in 5
out 1
out 4
in 7
Output
3
1
4
-1
4
-------------------------------------------------
不用 set ,二分可以试试
lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个不小于value 的值

[1]

  。该函数为C++ STL内的函数。
——百度百科
 1 #include <iostream>
2 #include <cstring>
3 #include <cmath>
4 #include <cstdio>
5 #include <iomanip>
6 #include <string>
7 #include <set>
8 #include <queue>
9 #include <stack>
10 #include <map>
11 #include <algorithm>
12
13 using namespace std;
14
15 typedef long long LL;
16 const int INF = 0x3f3f3f3f;
17 const int MAXN = 100005;
18 const int MOD = 1e9+7;
19
20 int main()
21 {
22 int n, Q;
23 cin >> n >> Q;
24 int desk[MAXN];
25 set<pair<int, int> > s;
26 set<pair<int, int> > :: iterator it;
27 for(int i = 1;i <= n;++i)
28 {
29 cin >> desk[i];
30 s.insert(make_pair(desk[i], i));
31 }
32 string str;
33 int t;
34 while(Q--)
35 {
36 cin >> str >> t;
37 if(str[0] == 'i')
38 {
39 it = s.lower_bound(make_pair(t, 0));
40 if(it == s.end())
41 cout << -1 << endl;
42 else
43 {
44 cout << it -> second << endl;
45 s.erase(it);
46 }
47 }
48 else
49 s.insert(make_pair(desk[t], t));
50 }
51 return 0;
52 }
 

-------------------------------------------

G

There are K hours left before Agent Mahone leaves Amman! Hammouri doesn't like how things are going in the mission and he doesn't want to fail again. Some places have too many students covering them, while other places have only few students.

Whenever Hammouri commands a student to change his place, it takes the student exactly one hour to reach the new place. Hammouri waits until he is sure that the student has arrived to the new place before he issues a new command. Therefore, only one student can change his place in each hour.

Hammouri wants to command the students to change their places such that after K hours, the maximum number of students covering the same place is minimized.

Given the number of students covering each place, your task is to find the maximum number of students covering the same place after K hours, assuming that Hammouri correctly minimizes this number.

Input

The first line of input contains two integers M (2 ≤ M ≤ 105) and K (1 ≤ K ≤ 109), where M is the number of places and K is the number of hours left before Agent Mahone leaves Amman.

The second line contains M non-negative integers, each represents the number of students covering one of the places. Each place is covered by at most 109 students.

Output

Print the maximum number of students covering a place after K hours, assuming that Hammouri minimized this number as much as possible in the last K hours.

Examples

Input
5 4
3 4 1 4 9
Output
5
Input
2 1000000000
1000000000 4
Output
500000002
Input
5 3
2 2 2 2 1
Output
2
---------------------------------------
二分,注意边界
刚开始,以平均值和最大值为边界,计算可移动的数目小于题目所给值即可
 1 #include <iostream>
2 #include <cstring>
3 #include <cmath>
4 #include <cstdio>
5 #include <iomanip>
6 #include <string>
7 #include <set>
8 #include <queue>
9 #include <stack>
10 #include <map>
11 #include <algorithm>
12
13 using namespace std;
14
15 typedef long long LL;
16 const int INF = 0x3f3f3f3f;
17 const int MAXN = 100005;
18 const int MOD = 1e9+7;
19
20 int main()
21 {
22 int n, i;
23 LL k, num[MAXN], mx = 0, sum = 0;
24 cin >> n >> k;
25 for(i = 0;i < n;++i)
26 {
27 cin >> num[i];
28 mx = max(mx, num[i]);
29 sum += num[i];
30 }
31 // int ave = mx / m;
32 // if(mx % m)
33 // ave++;
34 // 等同于下行码
35 LL ave = (sum + n - 1) / n;
36 LL l = ave, r = mx, mid, ans;
37 while(l <= r)
38 {
39 mid = (l + r) >> 1;
40 sum = 0;
41 for(i = 0;i < n;++i)
42 if(num[i] > mid)
43 sum += (num[i] - mid);
44 if(sum <= k)
45 {
46 ans = mid;
47 r = mid - 1;
48 }
49 else
50 l = mid + 1;
51 // cout << sum << endl;
52 }
53 cout << ans << endl;
54 return 0;
55 }

------------------------------------------------------------------

M

George met AbdelKader in the corridor of the CS department busy trying to fix a group of incorrect equations. Seeing how fast he is, George decided to challenge AbdelKader with a very large incorrect equation. AbdelKader happily accepted the challenge!

Input

The first line of input contains an integer N (2 ≤ N ≤ 300), the number of terms in the equation.

The second line contains N integers separated by a plus + or a minus -, each value is between 1 and 300.

Values and operators are separated by a single space.

Output

If it is impossible to make the equation correct by replacing operators, print  - 1, otherwise print the minimum number of needed changes.

Examples

Input
7
1 + 1 - 4 - 4 - 4 - 2 - 2
Output
3
Input
3
5 + 3 - 7
Output
-1
 1 #include <iostream>
2 #include <string>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <iomanip>
7 #include <stack>
8 #include <queue>
9 #include <set>
10 #include <map>
11
12 using namespace std;
13
14 typedef long long LL;
15 const int MAXN = 100005;
16 const int INF = 0x3f3f3f3f;
17 const int MOD = 1e9 + 7;
18
19 #define Mem0(x) memset(x, 0, sizeof(x));
20 #define MemM(x) memset(x, 0x3f, sizeof(x));
21 //01背包,第 i 个物品剩余容量为 j 时操作次数
22 int n, a[305], dp[305][180005], sum;
23 int main()
24 {
25 cin >> n >> a[1];
26 sum = a[1];
27 int i, j;
28 char c;
29 for(i = 2;i <= n;++i)
30 {
31 cin >> c >> a[i];
32 sum += a[i];
33 if(c == '-')
34 a[i] = - a[i];
35 }
36 if(sum % 2)
37 cout << -1 << endl;
38 else
39 {
40 MemM(dp);
41 dp[1][sum / 2 + a[1]] = 0;
42 for(i = 2;i <= n;++i)
43 for(j = 0;j <= sum;++j)
44 {
45 if(j >= a[i])
46 dp[i][j] = min(dp[i][j], dp[i - 1][j - a[i]]);
47 if(j >= -a[i])
48 dp[i][j] = min(dp[i][j], dp[i - 1][j + a[i]] + 1);
49 }
50
51 // cout << endl;
52 // for(i = 1;i <= n;++i)
53 // {
54 // for(j = 0;j <= sum;++j)
55 // cout << dp[i][j] << " ";
56 // cout << endl;
57 // }
58
59 if(dp[n][sum / 2] >= n)
60 cout << -1 << endl;
61 else
62 cout << dp[n][sum / 2] << endl;
63 }
64 return 0;
65 }

Gym - 100989的更多相关文章

  1. Gym - 100989 L / M 【dfs / dp】

    题目链接:http://codeforces.com/gym/100989/problem/L / http://codeforces.com/gym/100989/problem/M 题目大意:给定 ...

  2. set,pair容器使用方法

    题目链接:http://codeforces.com/gym/100989/problem/D In this cafeteria, the N tables are all ordered in o ...

  3. cf100989b

    http://codeforces.com/gym/100989/my B. LCS (B) time limit per test 0.25 seconds memory limit per tes ...

  4. dfs(枚举)

    http://codeforces.com/gym/100989/problem/L L. Plus or Minus (A) time limit per test 1.0 s memory lim ...

  5. 贪心(change)

    http://codeforces.com/gym/100989/problem/H After the data structures exam, students lined up in the ...

  6. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  7. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  8. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  9. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

随机推荐

  1. c语言split的实现代码

    我们知道在其他语言中有split函数可以把一个字符串按你自己想要的分隔符分割成多个字符串并以列表的形式返回.但是对于c语言来说,是没有这样一个函数接口可以直接调用的.但是有时候在项目工作中,又会用到这 ...

  2. 实践作业3:白盒测试----开始测试用例的设计DAY3

    白盒测试与黑盒测试很大不同之处在于白盒测试必须读相应代码,对代码有一定了解的情况下针对代码的逻辑进行测试用例的设计.白盒测试有六种覆盖标准:语句覆盖.判定覆盖.条件覆盖.判定/条件覆盖.条件组合覆盖和 ...

  3. Android 基于google Zxing实现对手机中的二维码进行扫描

    转载请注明出处:http://blog.csdn.net/xiaanming/article/details/14450809 有时候我们有这样子的需求,需要扫描手机中有二维码的的图片,所以今天实现的 ...

  4. python 读取mysql存储的文件路径下载文件,内容解析,上传七牛云,内容入es

    #!/usr/bin/env python # -*- coding: utf-8 -*- import ConfigParser import json import os import re fr ...

  5. C# 接口(3)

    这么半天说了如何使用,实现接口.相信也都发现了接口和抽象类很多相似的地方. 但是! 这两个根本就是不一样的. 抽象类 :                                         ...

  6. Spring AOP的实现机制

    AOP(Aspect Orient Programming),一般称为面向切面编程,作为面向对象的一种补充,用于处理系统中分布于各个模块的横切关注点,比如事务管理,日志,缓存等等.AOP 实现的关键在 ...

  7. 解决org.hibernate.NonUniqueObjectException的问题

    不知道是不是之前处理懒加载的问题对session工厂进行了处理,导致了之前没有问题的地方出现了错误. 当修改班级操作时出现了错误 前端错误信息 后台处理以及报错信息 16:37:36,034 ERRO ...

  8. 【转】plsql 永久注册码适用个版本

    源地址:https://blog.csdn.net/sinat_33142609/article/details/72540025 注册码:Product Code:4t46t6vydkvsxekkv ...

  9. rest_framwork中ApiView实现分页

    from rest_framework.pagination import PageNumberPagination from .serializers import BookSerilizer fr ...

  10. Thinkphp5.0 路由

    路由定义: 有两种方式: (1).动态注册: eg: Route::rule('hello','index/index/hello','GET'); (2)配置式: eg: return [ 'pat ...