锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 闃熷垪鍙互鍙岀鍑洪槦錛岄槦鍒楀彲浠ョ湅鎴愪竴鍗婃槸瀛樻斁榪囨湡鐨勫鹼紝涓鍗婃槸瀛樻斁瀵規湭鏉ユ湁鐢ㄧ殑鍊箋?/p>
浠[i]緇撳熬鐨刱紿楀彛鍐呯殑鏈澶у糵[i] = Max{a[i-k+1] ... a[i]};f[i+1] = Max{a[i+1-k+1]... a[i+1]}; 鎵浠ュ湪姹俧[i+1]鏃訛紝鍙互鍒╃敤姹俧[i]鏃剁瓫閫夊嚭瀵筬[i+1]鏈夋晥鐨勫箋?/p>
鍦ㄦ眰f[i]鏃訛紝鍦ㄥ尯闂碵i-k+1 + 1,i]鍐呯殑鍊兼墠瀵筬[i+1]鏈夋晥錛堝彲浠ュ湪闃熷垪浠庡ご寮濮嬪垽鏂笅鏍囨潵鍒犻櫎涓浜涜繃鏈熷鹼級銆傝屽湪榪欎釜鏈夋晥鍖洪棿鍐咃紝姣攁[i]灝忕殑鏁板f[i+1]涓嶆槸蹇呰鐨勶紝鎵浠ュ彧闇瑕佸瓨鏀綼[i]錛屾瘮a[i]澶х殑鏁板f[i+1]鍙兘鏄繀瑕佺殑錛屽氨涓嶈兘鍒犻櫎錛堥氳繃浠庨槦灝懼紑濮嬪垹闄ゆ瘮a[i]灝忕殑鍊?銆傚垹闄ゅソ鍚庣殑闃熷垪瀛樻斁鐨勬槸涓涓粠澶у埌灝忕殑鏈夋晥鍊煎簭鍒楋紝閭[i]灝辨槸闃熷ご鍏冪礌浜嗐?/p>
Max Sum of Max-K-sub-sequence
Now your job is to calculate the max sum of a
Max-K-sub-sequence. Max-K-sub-sequence means a continuous non-empty
sub-sequence which length not exceed K.
Then
T lines follow, each line starts with two integers N ,
K(1<=N<=100000 , 1<=K<=N), then N integers followed(all the
integers are between -1000 and 1000).
6 3
6 -1 2 -6 5 -5
6 4
6 -1 2 -6 5 -5
6 3
-1 2 -6 5 -5 6
6 6
-1 -1 -1 -1 -1 -1
7 1 3
7 6 2
-1 1 1
棰樻剰錛氱粰瀹氭瀯鎴愬渾鐜殑搴忓垪鍜屼竴涓猭紿楀彛錛屾眰(鏈澶х殑涓嶈秴榪噆闀垮害鐨勬渶澶у瓧孌靛拰) 涓?(瀛愭鍜岄暱搴﹀彇闀垮害鏈灝忕殑)銆?br>
#define maxn 200010
int num[maxn], que[maxn], sum[maxn], ans[maxn];
int main()
{
int t, n, k, m, i, j, a, b, c, p, max, ksum, len;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &k);
m = n;
for (i = 1; i <= n; i++)
{
scanf("%d", &num[i]);
}
for (j = 1; j < k; j++, i++)//鏋勬垚
n - 1, n, 1, 2,
, k - 1
{
num[i] = num[j];
}
n = i;
for (sum[0] = num[0] = 0, i = 1; i < n; i++)
{
sum[i] = sum[i-1] + num[i];
}
a = b = 0;
que[b++] = 0;//寮濮嬫湁涓兼槸0,闃熷垪閲屽瓨鏀劇殑鏄湁鏁堝拰sum鐨勪笅鏍?nbsp;
for (i = 1; i < n; i++)
{
for (; a < b && que[a] < i - k; a++);//鍒犻櫎榪囨湡鐨勫鹼紝i-k闄愬畾浜嗘渶澶у彲浠ュ彇k闀垮害
ans[i] = que[a];//瀵逛簬i緇撳熬鐨刱紿楀彛錛宖[i] = sum[i]-sum[que[a]]
for (; a < b && sum[que[b-1]] >= sum[i]; b--);//鍒犻櫎鏃犳晥鍊?nbsp;
que[b++] = i;
}
for (i = 1, max = -230000000; i < n; i++)
{
ksum = sum[i] - sum[ans[i]];
if (ksum > max)
{
max = ksum;
p = i;
len = i - ans[i];
}
else if (ksum == max && len > i - ans[i])
{
p = i;
len = i - ans[i];
}
}
c = ans[p] + 1;
if (c > m)
{
c -= m;
}
if (p > m)
{
p -= m;
}
printf("%d %d %d\n", max, c, p);
}
system("pause");
return 0;
}
]]>
Although those towers seem to be an
excellent air defense method, there is a problem: The area of the disk
generated by a tower is proportional to the amount of energy it
receives. The base has enough power plants to generate a certain amount
of energy, which has to be divided among those two towers. That means
that the total area of the two disks generated from the towers should
not exceed the total energy generated by the power plants. Fortunately,
the spy was able to know the exact target co-ordinates of the incoming
missiles and he reported them to General Gee. The General needs your
help in distributing the energy on the two magnetic towers to minimize
the number of missiles that will not get deflected by the magnetic
towers and therefore will hit the base. You may assume the following:
1.
The towers have different heights and therefore there are no problems
associated with the magnetic disks interfering with each other.
2.
A missile will deflect if it passes through the magnetic disk of a
tower or even if it just touches its boundary.
3. A missile
hitting a tower (landing exactly on its location) will deflect, even if
the tower is not given any energy.
4. All incoming missiles will
go down simultaneously at the exact instant; therefore, there will not
be any time available to redistribute the energy amongst the two towers
during the strike.
The absolute value
of all the given real numbers is less than or equal to 100 and may
include a decimal point followed by up to 3 digits. Any two consecutive
numbers on the same line are separated by one or more white-space
characters. Zero or more blank lines may appear between test cases.
The
last line of the input file is made of a single zero.
k. M
Where k is the test case number (starting at one,) and M
is the minimum number of missiles that will NOT be deflected in the
best distribution of energy among the two towers. Use π = 3.141.
Note:
There is a blank space before M.
-3 0 3 0 40.833
-1 4
-2 2.5
1 2
5 2
-4 0
-3 -1
2
0 0 1 1 0
0 0
1 1
0
2. 0
#include<stdlib.h>
#define maxn 1000
#define PI 3.141
const double inf = 0.00001;
double d1[maxn], d2[maxn];
double dis(double x1, double y1, double x2, double y2)
{
return 1.0 * (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
int main()
{
int n, i, j, ans, sum, k = 0;
double r1, r2, r3, r4, r;
double x1, y1, x2, y2, x, y;
while (scanf("%d", &n), n)
{
scanf("%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &r);
r = 1.0 * r / PI;
for (i = 0; i < n; i++)
{
scanf("%lf%lf", &x, &y);
d1[i] = dis(x, y, x1, y1);
d2[i] = dis(x, y, x2, y2);
}
for (i = 0, ans = n; i < n; i++)
{
r1 = d1[i];
if (r1 <= r)//鏋氫婦涓涓偣鍜屽叾涓竴涓鐨勮窛紱籸1,鍒欏墿涓嬬殑紓佹嘗璺濈涓簉2
{
r2 = r - r1;
for (j = 0, sum = 0; j < n; j++)
{
if (d1[j] <= r1)
{
sum++;
}
else if (d2[j] <= r2)
{
sum++;
}
}
if (ans > n - sum)
{
ans = n - sum;
}
}
r2 = d2[i];
if (r2 <= r)
{
r1 = r - r2;
for (j = 0, sum = 0; j < n; j++)
{
if (d1[j] <= r1)
{
sum++;
}
else if (d2[j] <= r2)
{
sum++;
}
}
if (ans > n - sum)
{
ans = n - sum;
}
}
}
k++;
printf("%d. %d\n", k, ans);
}
return 0;
}
]]>
You control an animated frog
that can walk and hop, in both forward and backward directions. The frog
stands in a space between an otherwise a contiguous line of tiles. Each
tile is painted black on one side, and white on the other. The frog can
walk (forward, or backward) over an adjacent tile (in front or behind
him.)
When the frog walks over a tile, the tile slides to the space
where the frog was standing. For example, in the adjacent figure, the
frog has two tiles behind him, and three in front. We’ll use the
notation BWFBBW to refer to this situation where F refers to the space
(where the frog is standing,) B is a tile with its black face showing,
while W is a tile with its white face showing. The forward direction is
from left to right. If the frog were to walk forward, the resulting
situation is BWBFBW. Similar behavior when the frog walks backward, the
tile behind the frog slides to where the frog was standing. The frog can
also hop over the tiles. The frog can hop over an adjacent tile landing
on the tile next to it. For example, if the frog was to hop backward,
it would land on the first (left-most) tile, and the tile would jump to
the space where the frog was standing. In addition, the tile would flip
sides. For example, hopping backward in the figure would result in the
situation: FWWBBW. We challenge you to write a program to determine the
minimum number of moves (walks or hops) to transform one tile
configuration into another.
k.
M
Where k is the test case number (starting at one,) and M is the
minimum number of moves needed to transform the given arrangement to an
arrangement that has no white tile(s) between any of its black tiles .
The frog can be anywhere. M is -1 if the problem cannot be solved in
less than 10 moves.
Note: There is a blank space before M.
WWFBWBW
FWBBWBW
---
2. 1
3. 2
棰樻剰錛氶潚铔欏彲鍚戝墠鎴栧悜鍚庤蛋涓姝ワ紝璧板悗錛岄潚铔欐墍鍦ㄤ綅緗殑鐮栨粦鍒伴潚铔欐病璺沖墠鐨勪綅緗傞潚铔欎篃鍙互鍚戝墠璺蟲垨鍚戝悗璺籌紝璺充竴嬈¤兘璺寵繃2涓爾錛?br>
闈掕洐鎵鍦ㄤ綅緗殑鐮栫炕杞悗婊戝埌闈掕洐娌¤煩鍓嶇殑浣嶇疆銆傛眰闈掕洐鍦ㄥ崄涓姩浣滀箣鍐咃紝浣夸換鎰忎袱涓粦鐮栦箣闂存病鐧界爾鐨勬渶灝忔鏁般?br>
#include<string.h>
#define maxn 300000
int ans, len;
char state[maxn][100];
char step[maxn], f[maxn];
int check(int depth)
{
int i, j, x, y;
for (i = 0, j = len - 1, x = y = -1; i < len; i++, j--)
{
if (state[depth][i] == 'B' && x == -1)
{
x = i;
}
if (state[depth][j] == 'B' && y == -1)
{
y = j;
}
}
if (x != -1 && y != -1)
{
for (i = x + 1; i < y; i++)
{
if (state[depth][i] == 'W')
{
return 0;
}
}
}
return 1;
}
void swap(int depth, int i, int j)
{
state[depth][i] ^= state[depth][j];
state[depth][j] ^= state[depth][i];
state[depth][i] ^= state[depth][j];
}
void hopf(int depth, int i)
{
swap(depth, i, i + 2);
state[depth][i] = (state[depth][i] == 'B') ? 'W' : 'B' ;
}
void hopb(int depth, int i)
{
swap(depth, i, i - 2);
state[depth][i] = (state[depth][i] == 'B') ? 'W' : 'B' ;
}
void bfs()
{
int head = 0, tial = 1, h;
if (check(0))
{
ans = 0;
return;
}
while (head < tial)
{
if (step[head] >= 9)
{
return;
}
h = f[head];
if (h + 1 < len)
{
strcpy(state[tial], state[head]);
swap(tial, h, h + 1);
step[tial] = step[head] + 1;
f[tial] = h + 1;
if (check(tial))
{
ans = step[tial];
return;
}
tial++;
}
if (h - 1 >= 0)
{
strcpy(state[tial], state[head]);
swap(tial, h, h - 1);
step[tial] = step[head] + 1;
f[tial] = h - 1;
if (check(tial))
{
ans = step[tial];
return;
}
tial++;
}
if (h + 2 < len)
{
strcpy(state[tial], state[head]);
hopf(tial, h);
step[tial] = step[head] + 1;
f[tial] = h + 2;
if (check(tial))
{
ans = step[tial];
return;
}
tial++;
}
if (h - 2 >= 0)
{
strcpy(state[tial], state[head]);
hopb(tial, h);
step[tial] = step[head] + 1;
f[tial] = h - 2;
if (check(tial))
{
ans = step[tial];
return;
}
tial++;
}
head++;
}
}
int main()
{
int i, j, k = 0;
char s[100];
while (scanf("%s", s), s[0] != '-')
{
k++;
ans = 10;
for (i = 0; (state[0][i] = s[i]) != 0; i++)
{
if (s[i] == 'F')
{
j = i;
}
}
len = i;
strcpy(state[0], s);
step[0] = 0;
f[0] = j;
bfs();
printf("%d. %d\n", k, ans < 10 ? ans : -1);
}
}
]]>