题意 某大参加ACM竞赛回来落下很多作业 每个作业都有最后期限 没在最后期限之内做完期末就要扣掉对应的分 求最少扣多少分

把所有作业按扣分大小从大到小排序 然后就贪阿 能完成前面的就完成前面的 实在不能的就扣分吧~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1005;
int dli[N], red[N], k[N], cas, ans, n;
bool vis[N];

bool cmp (int i, int j)
{
return red[i] > red[j];
}

int main()
{
scanf ("%d", &cas);
while (cas--)
{
ans = 0;
memset (vis, 0, sizeof (vis));
scanf ("%d", &n);
for (int i = 1; i <= n; ++i)
scanf ("%d", &dli[i]), k[i] = i;
for (int j = 1; j <= n; ++j)
scanf ("%d", &red[j]);

sort (k + 1, k + n + 1, cmp);
for (int i = 1, j; i <= n; ++i)
{
for (j = dli[k[i]]; j >= 1; --j)
if (!vis[j])
{
vis[j] = 1;
break;
}
if (j == 0) ans += red[k[i]];
}
printf ("%d\n", ans);
}
return 0;
}<span style="font-family:Comic Sans MS;">
</span>

Doing Homework again

Problem Description

Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
Input

The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.
Output

For each test case, you should output the smallest total reduced score, one line per test case.
Sample Input

3 3 3 3 3 10 5 1 3 1 3 1 6 2 3 7 1 4 6 4 2 4 3 3 2 1 7 6 5 4
Sample Output

0 3 5

题意 汽车通过渡船过河 渡船开始在左边 输入按车辆来的顺序输入河两岸的车 渡船每次运输的汽车的总长度不能超过渡船自己本身的长度 先来的车先走 求轮船至少跨河多少次才能将所有的车辆都运完

简单模拟 建两个队列 分别装左边的车 和右边的车 算出两边各至少需要运输多少次就行了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int main()
{
int cas, lcnt, rcnt, on,n,m,l;
char s[10];
scanf ("%d", &cas);
while (cas--)
{
queue<int> le, ri;
scanf ("%d%d", &n, &m);
n *= 100;
while (m--)
{
scanf ("%d%s", &l, s);
if (s[0] == 'l') le.push (l);
else ri.push (l);
}
lcnt = on = 0;
while (!le.empty())
{
while (!le.empty() && on + le.front() < n)
on += le.front(), le.pop();
++lcnt, on = 0;
}
rcnt = on = 0;
while (!ri.empty())
{
while (!ri.empty() && on + ri.front() < n)
on += ri.front(), ri.pop();
++rcnt, on = 0;
}
if (lcnt > rcnt) printf ("%d\n", 2 * lcnt - 1);
else printf ("%d\n",2 * rcnt);
}
return 0;
}

Ferry Loading IV

Description
Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river’s current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.

There is an l-meter-long ferry that crosses the river. A car may arrive at either river bank to be transported by the ferry to the opposite bank. The ferry travels continuously back and forth between the banks so long as it is carrying a car or there is at least one car waiting at either bank. Whenever the ferry arrives at one of the banks, it unloads its cargo and loads up cars that are waiting to cross as long as they fit on its deck. The cars are loaded in the order of their arrival; ferry’s deck accommodates only one lane of cars. The ferry is initially on the left bank where it broke and it took quite some time to fix it. In the meantime, lines of cars formed on both banks that await to cross the river.

Input

The first line of input contains c, the number of test cases. Each test case begins with l, m. m lines follow describing the cars that arrive in this order to be transported. Each line gives the length of a car (in centimeters), and the bank at which the car arrives (“left” or “right”).

Output

For each test case, output one line giving the number of times the ferry has to cross the river in order to serve all waiting cars.

Sample Input

4 20 4 380 left 720 left 1340 right 1040 left 15 4 380 left 720 left 1340 right 1040 left 15 4 380 left 720 left 1340 left 1040 left 15 4 380 right 720 right 1340 right 1040 right

Sample Output

3 3 5 6

题意 给两堆牌s1,s2交给你洗 每堆有c张 每次洗牌得到s12 其中s2的最下面一张在s12的最下面一张然后按顺序一张s1一张s2 洗好之后可以把s12下面的c张做s1 上面的c张做s2 求多少次洗牌之后可以得到输入给你的串s 不能得到输出-1

简单模拟 s1+s2!=s就一直洗牌 如果回到初始状态都没得到s就不会得到s了 得到s就可以输出洗牌次数了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<string>
using namespace std;
int cas, c, ans;
string s, bs, s12, s1, s2;

void shuf (string s1, string s2)
{
if (bs == s) return;
++ans;
int i = -1, j = -1, k;
string ss1, ss2;
for (k = 0; k < c; ++k)
ss1 += (i == j ? s2[++i] : s1[++j]);
for (k = 0; k < c; ++k)
ss2 += (i == j ? s2[++i] : s1[++j]);
s12 = ss1 + ss2;
if (s12 == s || s12 == bs) return;
else shuf (ss1, ss2);
}

int main()
{
cin >> cas;
for (int ca = 1; ca <= cas; ++ca)
{
ans = 0;
cin >> c >> s1 >> s2 >> s;
bs = s1 + s2;
shuf (s1, s2);
if (s12 != s) ans = -1;
cout << ca << " " << ans << endl;
}
return 0;
}

也可以用bfs来做 每次出队入队的也只有一个 和模拟差不多

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int N = 105;
string q[N], ss, ts;
int n;

int bfs()
{
string cs, s;
int le = 0, ri = 0;
q[ri++] = ss;
while(le < ri)
{
cs = q[le++];
if(cs == ts) return le - 1;
s = "";
for(int i = 0; i < n; ++i)
s += cs[n + i], s += cs[i];
if(s == ss) return -1;
q[ri++] = s;
}
return -1;
}

int main()
{
int cas;
char a[N], b[N], s[2 * N];
scanf("%d", &cas);
for(int k = 1; k <= cas; ++k)
{
scanf("%d%s%s%s", &n, a, b, s);
ts = string(s), ss = string(a) + b;
printf("%d %d\n", k, bfs());
}
return 0;
}

附上此题数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
11
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC
9
ACCABCABC
DEFDEFDEF
ECECECAFAFAFDBDBDB
10
ABCDEFGHCD
AAAAAAAAAA
BDFHDAAAAAACEGCAAAAA
100
ABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCD
ABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCD
CEGCACEGCACEGCACEGCACEGCADFHDBDFHDBDFHDBDFHDBDFHDBEGCACEGCACEGCACEGCACEGCACFHDBDFHDBDFHDBDFHDBDFHDBDGCACEGCACEGCACEGCACEGCACEHDBDFHDBDFHDBDFHDBDFHDBDFCACEGCACEGCACEGCACEGCACEGDBDFHDBDFHDBDFHDBDFHDBDFH
5
AAAAA
BBBBB
AABBBAAABB
6
AAAAAA
BBBBBB
AAABBBAAABBB
7
AAAAAAA
BBBBBBB
ABBAABBAABBAAB
8
AAAAAAAA
BBBBBBBB
BBAABBAABBAABBAA
9
AAAAAAAAA
BBBBBBBBB
BBBBAAAAABBBBBAAAA
10
AAAAAAAAAA
BBBBBBBBBB
BBAABBAABBAABBAABBAA

1 2
2 -1
3 -1
4 5
5 30
6 9
7 11
8 2
9 2
10 8
11 2
*/

Shuffle’m Up

Description
A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing Cchips. Each stack may contain chips of several different colors.

The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:

The single resultant stack, S12, contains 2 /* C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.

After the shuffle operation, S12 is split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new S2. The shuffle operation may then be repeated to form a new S12.

For this problem, you will write a program to determine if a particular resultant stack S12 can be formed by shuffling two stacks some number of times.

Input

The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.

Each dataset consists of four lines of input. The first line of a dataset specifies an integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 /* C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S1 and S2 zero or more times. The bottommost chip’s color is specified first.

Output

Output for each dataset consists of a single line that displays the dataset number (1 though N), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (−1) for the number of shuffle operations.

Sample Input

2 4 AHAH HAHA HHAAAAHH 3 CDE CDE EEDDCC

Sample Output

1 2 2 -1

题意 模拟银行的排队系统 有三种操作 1-添加优先级为p 编号为k的人到队列 2-服务当前优先级最大的 3-服务当前优先级最小的 0-退出系统

可以用stl中的map 因为map本身就根据key的值排了序 对应2,3 我们只需要输出最大或最小就行了并从map中删除该键值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<cstdio>
#include<map>
using namespace std;
map<int, int> a;
int main()
{
map<int, int>::iterator it;
int n,k,p;
while (scanf ("%d", &n), n)
{
if (n == 1)
{
scanf ("%d%d", &k, &p);
a[p] = k;
}
else
{
if (a.empty()) printf ("0\n");
else
{
if(n==2) it = a.end(),--it;
else it=a.begin();
printf ("%d\n", it->second);
a.erase (it->first);
}
}
}
return 0;
}

Double Queue

Description
The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:
0 The system needs to stop serving 1 K P Add client K to the waiting list with priority P 2 Serve the client with the highest priority and drop him or her from the waiting list 3 Serve the client with the lowest priority and drop him or her from the waiting list

Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

Input

Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

Output

For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

Sample Input

2 1 20 14 1 30 3 2 1 10 99 3 2 2 0

Sample Output

0 20 30 10 0

题意 图中每个矩形’/#’连通块代表一艘船 若一艘船与另一艘有边相邻或有角相邻 那么认为这两艘船相撞 若图中有船相撞 输出bad 否则输出图中有多少艘船

可以把图的周围全包上一圈’.’ 遍历图中每个点 可知当图中存在一下四种结构中的一个时 必有船相撞 输出并退出循环 否则则dfs这个点 若图中不存在这些结构 就可以输出连通块数量即轮船数了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<span style="font-family:Microsoft YaHei;">#include<cstdio>
#include<cstring>
using namespace std;

const int N=1005;
char mat[N][N];bool vis[N][N];
int x[4]= {-1,1,0,0},y[4]= {0,0,-1,1};

bool isBad(int i,int j)
{
if(mat[i][j]=='.'&&mat[i-1][j]=='#'&&mat[i][j-1]=='#') return 1;
if(mat[i][j]=='.'&&mat[i-1][j]=='#'&&mat[i][j+1]=='#') return 1;
if(mat[i][j]=='.'&&mat[i+1][j]=='#'&&mat[i][j-1]=='#') return 1;
if(mat[i][j]=='.'&&mat[i+1][j]=='#'&&mat[i][j+1]=='#') return 1;
return 0;
}

int dfs(int i,int j)
{
if (vis[i][j]||mat[i][j]=='.') return 0;
vis[i][j]=true;
for(int k=0; k<4; ++k)
if(mat[i+x[k]][j+y[k]]=='#') dfs(i+x[k],j+y[k]);
return 1;
}

int main()
{
int i,j,n,m;
while(scanf("%d%d",&n,&m),n)
{
for(i=1; i<=n; ++i)
scanf("%s",mat[i]+1);
for(i=0; i<=n+1; ++i) mat[i][0]=mat[i][m+1]='.';
for(j=0; j<=m+1; ++j) mat[0][j]=mat[n+1][j]='.';
int ans=0;
memset(vis,0,sizeof(vis));
for(i=1; i<=n; ++i)
{
for(j=1; j<=m; ++j)
if(isBad(i,j)) break;
else ans+=dfs(i,j);
if (j<=m) break;
}

if(i<=n)
{
printf("Bad placement.\n");
continue;
}

printf("There are %d ships.\n",ans);
}
return 0;
}
</span>

Sea Battle

Description
During the Summit, the armed forces will be highly active. The police will monitor Prague streets, the army will guard buildings, the Czech air space will be full of American F-16s. Moreover, the ships and battle cruisers will be sent to guard the banks of the Vltava river. Unfortunately, in the case of any incident, the Czech Admiralty have only a few captains able to control over the large sea battle. Therefore, it was decided to educate new admirals. As an excellent preparation, the game of “Sea Battle” was chosen to help with their study program.
In this well-known game, a predefined number of ships of predefined shapes are placed on the square board in such a way that they cannot contact one another even with their corners. In this task, we will consider rectangular shaped ships only. The unknown number of rectangular ships of unknown sizes are placed on a rectangular board. All the ships are full rectangles built of hash characters. Write a program that counts the total number of ships present in the field.

Input

The input consists of more scenarios. The description of each scenario begins with two integer numbers R and C separated with a single space, 1 <= R,C <= 1000. These numbers give the number of rows and columns in the game field.
After these two numbers, there are R lines, each of them containing C characters. Each character is either hash (“/#”) or dot (“.”). Hashes denote ships, dots water.
Then, the next scenario description begins. At the end of the input, there will be a line containing two zeros instead of the field size.

Output

Output a single line for every scenario. If the ships were placed correctly (i.e., there are only rectangles that do not touch each other even with a corner), print the sentence “There are S ships.” where S is the number of ships.
Otherwise, print the sentence “Bad placement.”.

Sample Input

6 6 …../# /#/#…/# /#/#…/# ../#../# …../# /#/#/#/#/#/# 6 8 …../#./# /#/#…../# /#/#…../# ……./# /#……/# /#../#…/# 0 0

Sample Output

Bad placement. There are 5 ships.

题意 建设一条河岸的污水处理系统 河岸有n个城市 每个城市都可以自己处理污水 V 也可以把污水传到相邻的城市处理 >或< 除了你传给我我也传给你这种情况 其它都是合法的 两端的城市不能传到不存在的城市

令d[i]表示有i个城市时的处理方法数 最后一个城市的处理方法有

1.V 自己处理自己的 与前i-1个城市的处理方法无关 有d[i-1]种方法

2.< 给左边的城市去处理 也与前i-1个城市的处理方法无关 把自己的污水给第i-1个城市就行了 有d[i-1]种方法

3.>V 左边有污水传过来 和自己的一起处理 这时第i-1个城市可以向右传了 如果这种情况发生的话 那么第i-1个城市肯定不可能是向左传的 但前i-2个城市的处理方法没有影响 所以第i-1个城市向左传的方法有d[i-2]种 然后第i-1个城市不向左传就有d[i-1]-d[i-2]种方法了

所以有递推公式d[i]=3/*d[i-1]-d[i-2];

增长速度很快要用大数处理 大数就用java了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.*;
import java.math.*;

public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
BigInteger d[] = new BigInteger[105];
d[1] = BigInteger.ONE;
d[2] = BigInteger.valueOf(3);
for (int i = 3; i <= 100; ++i)
d[i] = d[i - 1].multiply(d[2]).subtract(d[i - 2]);
while (in.hasNext())
System.out.println(d[in.nextInt()]);
in.close();
}
<span style="font-family:Microsoft YaHei;">}</span>

还有没加大数模版的c++代码

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 105;
int main()
{
int d[N] = {0, 1, 3}, n;
for (int i = 3; i <= 100; ++i)
d[i] = 3 * d[i - 1] - d[i - 2];
while (~scanf ("%d", &n))
printf ("%d\n", d[n]);
return 0;
}

Water Treatment Plants

Description

River polution control is a major challenge that authorities face in order to ensure future clean water supply. Sewage treatment plants are used to clean-up the dirty water comming from cities before being discharged into the river.
As part of a coordinated plan, a pipeline is setup in order to connect cities to the sewage treatment plants distributed along the river. It is more efficient to have treatment plants running at maximum capacity and less-used ones switched off for a period. So, each city has its own treatment plant by the river and also a pipe to its neighbouring city upstream and a pipe to the next city downstream along the riverside. At each city’s treatment plant there are three choices:


The choices above ensure that:
every city must have its water treated somewhere and
at least one city must discharge the cleaned water into the river.
Let’s represent a city discharging water into the river as “V” (a downwards flow), passing water onto its neighbours as “>” (to the next city on its right) or else “<” (to the left). When we have several cities along the river bank, we assign a symbol to each (V, < or >) and list the cities symbols in order. For example, two cities, A and B, can
each treat their own sewage and each discharges clean water into the river. So A’s action is denoted V as is B’s and we write “VV” ;
or else city A can send its sewage along the pipe (to the right) to B for treatment and discharge, denoted “>V”;
or else city B can send its sewage to (the left to) A, which treats it with its own dirty water and discharges (V) the cleaned water into the river. So A discharges (V) and B passes water to the left (<), and we denote this situation as “V<”.
We could not have “><” since this means A sends its water to B and B sends its own to A, so both are using the same pipe and this is not allowed. Similarly “<<” is not possible since A’s “<” means it sends its water to a non-existent city on its left.
So we have just 3 possible set-ups that fit the conditions:
A B A > B A < B V V V V RIVER~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~RIVER “VV” “>V” “V<”
If we now consider three cities, we can determine 8 possible set-ups.
Your task is to produce a program that given the number of cities NC (or treatment plants) in the river bank, determines the number of possible set-ups, NS, that can be made according to the rules define above.
You need to be careful with your design as the number of cities can be as large as 100.

Input

The input consists of a sequence of values, one per line, where each value represents the number of cities.

Output

Your output should be a sequence of values, one per line, where each value represents the number of possible set-ups for the corresponding number of cities read in the same input line.

Sample Input

2 3 20

Sample Output

3 8 102334155

题意 中文 但要注意小于你能量的点也是能到达的

令d[i][j]表示到达第i行第j列的方法数 初始化为0 d[1][1]为1 输入一个点的能量t后 枚举这个点能到的所有点(i+x,j+y)(x+y<=t) 有d[i+x][j+y]+=d[i][j] 因为只能向右走和向下走 可以保证每次更新后均为当前最优解 输入最后一个点后 就得到答案了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 105, MOD = 10000;
int main()
{
int d[N][N], n, m, cas, t;
scanf ("%d", &cas);
while (cas--)
{
scanf ("%d%d", &n, &m);
memset (d, 0, sizeof (d)), d[1][1] = 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
{
scanf ("%d", &t);
for (int x = 0; x <= t && i + x <= n; ++x)
for (int y = 0; x + y <= t && j + y <= m; ++y)
{
if (x == 0 && y == 0) continue;
d[i + x][j + y] = (d[i][j] + d[i + x][j + y]) % MOD;
}
}
printf ("%d\n", d[n][m]);
}
return 0;
}

How many ways

Problem Description

这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m)。游戏的规则描述如下:
1.机器人一开始在棋盘的起始点并有起始点所标有的能量。
2.机器人只能向右或者向下走,并且每走一步消耗一单位能量。
3.机器人不能在原地停留。
4.当机器人选择了一条可行路径后,当他走到这条路径的终点时,他将只有终点所标记的能量。

如上图,机器人一开始在(1,1)点,并拥有4单位能量,蓝色方块表示他所能到达的点,如果他在这次路径选择中选择的终点是(2,4)
点,当他到达(2,4)点时将拥有1单位的能量,并开始下一次路径选择,直到到达(6,6)点。
我们的问题是机器人有多少种方式从起点走到终点。这可能是一个很大的数,输出的结果对10000取模。
Input

第一行输入一个整数T,表示数据的组数。
对于每一组数据第一行输入两个整数n,m(1 <= n,m <= 100)。表示棋盘的大小。接下来输入n行,每行m个整数e(0 <= e < 20)。
Output

对于每一组数据输出方式总数对10000取模的结果.
Sample Input

1 6 6 4 5 6 6 4 3 2 2 3 1 7 2 1 1 4 6 2 7 5 8 4 3 9 5 7 6 6 2 1 5 3 1 1 3 7 2
Sample Output

3948

题意 给你两个字符串p和s 求p在s中出现的次数 很裸的kmp

因为不止匹配一次 每次找到后还要循环j=next[j]的过程 知道到达s的终点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 10005, M = 1000005;
int next[N], ans, n;
char p[N], s[M];

void kmp ()
{
    int lp = strlen (p), ls = strlen (s);
    int i, j;

    //求next数组
    i = 0;
    j = next[0] = -1;
    while (i < lp)
    {
        if(j == -1 || p[i] == p[j])
            next[++i] = ++j;
        else j = next[j];
    }

//kmp匹配
    i = j = 0;
    while (i < ls)
    {
        if (j == -1 || s[i] == p[j])
        {
            ++i, ++j;
            if (j == lp) ++ans, j = next[j];
        }
        else j = next[j];
    }
}

int main()
{
    scanf ("%d", &n);
    while (n--)
    {
        ans = 0;
        scanf ("%s%s", p, s);
        kmp ();
        printf ("%d\n", ans);
    }
    return 0;
}

Oulipo

Description
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ‘e’. He was a member of the Oulipo group. A quote from the book:
Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive ‘T’s is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {‘A’, ‘B’, ‘C’, …, ‘Z’} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

Sample Input

3 BAPC BAPC AZA AZAZAZA VERDI AVERDXIVYERDIAN

Sample Output

1 3 0

题意 所有只能被2,3,5,7这4个素数整除的数称为Humble Number 输入n 输出第n个Humble Number

1是第一个humble number 对于一个Humble Number a 有2/a,3/a,5/a,7/a都是Humble Number 可以以1为基数 依次展开即可得到一定范围内的Humble Number 用i,j,k,l分别记录 2,3,5,7分别乘到了第几个Humble Number 当前在计算第cnt个Humble Number 那么有 hum[cnt] = min ( hum[i] / 2, hum[j] / 3, hum[k] / 5, hum[l] / 7) 然后对应min的i或j或k或l就加1 当cnt到达了n 结果就出来了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 5843;
int hum[N], cnt, n;
int main()
{
int i = 1, j = 1, k = 1, l = hum[1] = 1;
for (cnt = 2; cnt < N; ++cnt)
{
hum[cnt] = min ( min(hum[i] * 2, hum[j] * 3), min (hum[k] * 5, hum[l] * 7));
if (hum[cnt] == hum[i] * 2) ++i;
if (hum[cnt] == hum[j] * 3) ++j;
if (hum[cnt] == hum[k] * 5) ++k;
if (hum[cnt] == hum[l] * 7) ++l;
}
while (scanf ("%d", &n), n)
{
printf ("The %d", n);
if (n % 100 != 11 && n % 10 == 1) printf ("st ");
else if (n % 100 != 12 && n % 10 == 2) printf ("nd ");
else if (n % 100 != 13 && n % 10 == 3) printf ("rd ");
else printf ("th ");
printf ("humble number is %d.\n", hum[n]);
}
return 0;
}

Humble Numbers

Problem Description

A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, … shows the first 20 humble numbers.
Write a program to find and print the nth element in this sequence
Input

The input consists of one or more test cases. Each test case consists of one integer n with 1 <= n <= 5842. Input is terminated by a value of zero (0) for n.
Output

For each test case, print one line saying “The nth humble number is number.”. Depending on the value of n, the correct suffix “st”, “nd”, “rd”, or “th” for the ordinal number nth has to be used like it is shown in the sample output.
Sample Input

1 2 3 4 11 12 13 21 22 23 100 1000 5842 0
Sample Output

The 1st humble number is 1. The 2nd humble number is 2. The 3rd humble number is 3. The 4th humble number is 4. The 11th humble number is 12. The 12th humble number is 14. The 13th humble number is 15. The 21st humble number is 28. The 22nd humble number is 30. The 23rd humble number is 32. The 100th humble number is 450. The 1000th humble number is 385875. The 5842nd humble number is 2000000000.

题意 吃豆子游戏 当你吃了一个格子的豆子 该格子左右两个和上下两行就不能吃了 输入每个格子的豆子数 求你最多能吃多少颗豆子

可以先求出每行你最多可以吃多少颗豆子 然后每行就压缩成只有一个格子了 里面的豆子数就是那一行最多可以吃的豆子数 然后问题就变成求一列最多可以吃多少颗豆子了 和处理每一行一样处理 那么问题就简化成求一行数字的最大不连续和问题了

令d[i]表示某一行前i个豆子的最大和 有两种情况 吃第i个格子中的豆子和不吃第i个格子中的豆子 a[i]为第i个格子中的豆子数

吃 d[i]=d[i-2]+a[i] 不吃 d[i]=d[i-1]

所以有转移方程 d[i]=max(d[i-2]+a[i],d[i-1])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2014;
int d[N],row[N],col[N],mat[N][N],n,m;
int main()
{
while (~scanf ("%d%d", &n, &m))
{
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
scanf ("%d", &mat[i][j]);
memset (row, 0, sizeof (row));
for (int i = 1; i <= n; ++i)
{
memset (d, 0, sizeof (d));
d[1] = mat[i][1];
for (int j = 2; j <= m; ++j)
d[j] = max (d[j - 1], d[j - 2] + mat[i][j]);
row[i] = max (row[i], d[m]);
}
memset (col, 0, sizeof (col));
col[1] = row[1];
for (int i = 2; i <= n; ++i)
col[i] = max (col[i - 1], col[i - 2] + row[i]);
printf ("%d\n", col[n]);
}
return 0;
}

Beans

Problem Description

Bean-eating is an interesting game, everyone owns an M/*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1/*1 grid. Now you want to eat the beans and collect the qualities, but everyone must obey by the following rules: if you eat the bean at the coordinate(x, y), you can’t eat the beans anyway at the coordinates listed (if exiting): (x, y-1), (x, y+1), and the both rows whose abscissas are x-1 and x+1.
Now, how much qualities can you eat and then get ?
Input

There are a few cases. In each case, there are two integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of bean isn’t beyond 1000, and 1<=M/*N<=200000.
Output

For each case, you just output the MAX qualities you can eat and then get.
Sample Input

4 6 11 0 7 5 13 9 78 4 81 6 22 4 1 40 9 34 16 10 11 22 0 33 39 6
Sample Output

242
其实上面的代码还是有 bug的 题目说的是N/*M<=200000 并没有说N和M的具体范围 这里给出一位数组读入的版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 200010;
int d[N], row[N], col[N], mat[N], n, m;
int main()
{
while (~scanf ("%d%d", &n, &m))
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
scanf ("%d", &mat[i * m + j]);

for (int i = 0; i < n; ++i)
{
d[0] = mat[i * m + 0], d[1] = max (mat[i * m + 1], d[0]);
for (int j = 2; j < m; ++j)
d[j] = max (d[j - 1], d[j - 2] + mat[i * m + j]);
row[i] = d[m - 1];
}

col[0] = row[0], col[1] = max (row[0], row[1]);
for (int i = 2; i < n; ++i)
col[i] = max (col[i - 1], col[i - 2] + row[i]);
printf ("%d\n", col[n - 1]);
}
return 0;
}
0%