锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Trie鏍戝氨鏄瓧絎︽爲錛屽叾鏍稿績鎬濇兂灝辨槸絀洪棿鎹㈡椂闂?/p> 涓句釜綆鍗曠殑渚嬪瓙銆?/p> 緇欎綘100000涓暱搴︿笉瓚呰繃10鐨勫崟璇嶃傚浜庢瘡涓涓崟璇嶏紝鎴戜滑瑕佸垽鏂粬鍑烘病鍑虹幇榪囷紝濡傛灉鍑虹幇浜嗭紝絎竴嬈″嚭鐜扮鍑犱釜浣嶇疆銆?/p> 榪欓褰撶劧鍙互鐢╤ash鏉ワ紝浣嗘槸鎴戣浠嬬粛鐨勬槸trie鏍戙傚湪鏌愪簺鏂歸潰瀹冪殑鐢ㄩ旀洿澶с傛瘮濡傝瀵逛簬鏌愪竴涓崟璇嶏紝鎴戣璇㈤棶瀹冪殑鍓嶇紑鏄惁鍑虹幇榪囥傝繖鏍穐ash灝變笉濂芥悶浜嗭紝鑰岀敤trie榪樻槸寰堢畝鍗曘?/p> 鐜板湪鍥炲埌渚嬪瓙涓紝濡傛灉鎴戜滑鐢ㄦ渶鍌葷殑鏂規硶錛屽浜庢瘡涓涓崟璇嶏紝鎴戜滑閮借鍘繪煡鎵懼畠鍓嶉潰鐨勫崟璇嶄腑鏄惁鏈?瀹冦傞偅涔堣繖涓畻娉曠殑澶嶆潅搴﹀氨鏄疧(n^2)銆傛樉鐒跺浜?00000鐨勮寖鍥撮毦浠ユ帴鍙椼傜幇鍦ㄦ垜浠崲涓濊礬鎯熾傚亣璁炬垜瑕佹煡璇㈢殑鍗曡瘝鏄痑bcd錛岄偅涔堝湪浠栧墠闈㈢殑鍗?璇嶄腑錛屼互b錛宑錛宒錛宖涔嬬被寮澶寸殑鎴戞樉鐒朵笉蹇呰冭檻銆傝屽彧瑕佹壘浠寮澶寸殑涓槸鍚﹀瓨鍦╝bcd灝卞彲浠ヤ簡銆傚悓鏍風殑錛屽湪浠寮澶翠腑鐨勫崟璇嶄腑錛屾垜浠彧瑕佽冭檻浠浣滀負 絎簩涓瓧姣嶇殑……榪欐牱涓涓爲鐨勬ā鍨嬪氨娓愭笎娓呮櫚浜?#8230;… 鍋囪鏈塨錛宎bc錛宎bd錛宐cd錛宎bcd錛宔fg錛宧ii榪?涓崟璇嶏紝鎴戜滑鏋勫緩鐨勬爲灝辨槸榪欐牱鐨勩?/p> 瀵逛簬姣忎竴涓妭鐐癸紝浠庢牴閬嶅巻鍒頒粬鐨勮繃紼嬪氨鏄竴涓崟璇嶏紝濡傛灉榪欎釜鑺傜偣琚爣璁頒負綰㈣壊錛屽氨琛ㄧず榪欎釜鍗曡瘝瀛樺湪錛屽惁鍒欎笉瀛樺湪銆?/p> 閭d箞錛屽浜庝竴涓崟璇嶏紝鎴戝彧瑕侀『鐫浠栦粠璺熻蛋鍒板搴旂殑鑺傜偣錛屽啀鐪嬭繖涓妭鐐規槸鍚﹁鏍囪涓虹孩鑹插氨鍙互鐭ラ亾瀹冩槸鍚﹀嚭鐜拌繃浜嗐傛妸榪欎釜鑺傜偣鏍囪涓虹孩鑹詫紝灝辯浉褰撲簬鎻掑叆浜嗚繖涓崟璇嶃?/p> 榪欐牱涓鏉ユ垜浠闂拰鎻掑叆鍙互涓璧峰畬鎴愶紝鎵鐢ㄦ椂闂翠粎浠呬負鍗曡瘝闀垮害錛屽湪榪欎竴涓牱渚嬶紝渚挎槸10銆?/p> 鎴戜滑鍙互鐪嬪埌錛宼rie鏍戞瘡涓灞傜殑鑺傜偣鏁版槸26^i綰у埆鐨勩傛墍浠ヤ負浜嗚妭鐪佺┖闂淬傛垜浠敤鍔ㄦ侀摼琛紝鎴栬呯敤鏁扮粍鏉ユā鎷熷姩鎬併傜┖闂寸殑鑺辮垂錛屼笉浼氳秴榪囧崟璇嶆暟×鍗曡瘝闀垮害銆?/p> void Merge(int a, int b, int p, int q, int d)
N*N鐨勭煩闃甸噷錛屼袱縐嶇鎿嶄綔
A x y w錛宎[x][y]鍔犱笂w
S x1 y1 x2 y2錛?x1,y1)鍒?x2,y2)榪欎釜灝忕煩闃甸噷鎵鏈夋暟鐨勫拰
浜岀淮綰挎鏍?涓緇村x浜屽垎錛屾瘡涓妭鐐逛篃鏄竴涓嚎孌墊爲錛屽y浜屽垎銆?br />
int l,r,m;
int sum;
int num;
int pos;//treeX閲屾瘡涓妭鐐圭殑綰挎鏍戝湪treeY閲岀殑浣嶇疆
}treeX[maxn*2],treeY[maxn*2][maxn*2];
int N;
int a[maxn][maxn];
int s[maxn][maxn];//s[i][j]琛ㄧず錛?錛?錛夊埌(i,j)瀛愮煩闃電殑鍜?/span>
{
treeY[cnt][root].l = l;
treeY[cnt][root].r = r;
treeY[cnt][root].sum = s[rr][r]-s[rr][l-1]-s[ll-1][r]+s[ll-1][l-1];
if (l<r)
{
buildY(cnt,ll,rr,2*root,l,(l+r)/2);
buildY(cnt,ll,rr,2*root+1,(l+r)/2+1,r);
}
return ;
}
void buildX(int cnt,int root,int l,int r)
{
buildY(cnt,l,r,1,1,N);
treeX[root].l = l;
treeX[root].r = r;
treeX[root].sum = s[r][N]-s[l-1][N];
treeX[root].pos = cnt;
if (l<r)
{
buildX(cnt+1,2*root,l,(l+r)/2);
buildX(cnt+2,2*root+1,(l+r)/2+1,r);
}
return ;
}
int searchY(int cnt,int root,int l,int r)
{
if (l<=treeY[cnt][root].l && r>=treeY[cnt][root].r)
return treeY[cnt][root].sum;
int sum_l = 0, sum_r = 0;
int mid = (treeY[cnt][root].r + treeY[cnt][root].l)/2;
if (l<=mid)
sum_l = searchY(cnt,2*root,l,r);
if (r>mid)
sum_r = searchY(cnt,2*root+1,l,r);
return sum_l + sum_r;
}
int searchX(int root,int ll,int rr,int l,int r)
{
if (ll<=treeX[root].l && rr>=treeX[root].r)
return searchY(treeX[root].pos,1,l,r);
int sum_l = 0,sum_r = 0;
int mid = (treeX[root].l+treeX[root].r)/2;
if (ll<=mid)
sum_l = searchX(2*root,ll,rr,l,r);
if (rr>mid)
sum_r = searchX(2*root+1,ll,rr,l,r);
return sum_l + sum_r;
}
void addY(int cnt,int root,int y,int w)
{
treeY[cnt][root].sum += w;
if (treeY[cnt][root].l ==treeY[cnt][root].r)
return ;
int mid = (treeY[cnt][root].l + treeY[cnt][root].r)/2;
// puts("y");
if (y<=mid)
addY(cnt,2*root,y,w);
else
addY(cnt,2*root+1,y,w);
return ;
}
void addX(int root,int x,int y,int w)
{
treeX[root].sum += w;
addY(treeX[root].pos,1,y,w);
if (treeX[root].l == treeX[root].r)
return ;
// puts("x");
int mid = (treeX[root].l+treeX[root].r)/2;
if (x<=mid)
addX(2*root,x,y,w);
else
addX(2*root+1,x,y,w);
}
C鍜孋++寮卞晩錛岃鍐欎釜妯$増鐨勩?br />
]]>![]()
]]>
int father;鏍硅妭鐐?br /> int relation;涓庢牴鑺傜偣鍏崇郴:0 鍚岀被錛?鍚冩牴鑺傜偣錛?琚悆
} line[maxn];
int find(int x)
{
if (x!=line[x].father)
{
int tmp=line[x].father;
line[x].father=find(tmp);
//鎵捐寰嬶紝姹傚嚭鍏紡
num[x]=(line[x].relation+line[tmp].relation) % 3;
}
returen line[x].father;
}
void union(int x,int y,int d)
{
int fx=find(x);
int fy=find(y);
line[fx].father=fy;
//鎵捐寰嬶紝姹傚嚭鍏紡
line[fx].relation=(line[y].relation-line[x].relation+2+d) % 3;
return ;
}
Thinking && Coding錛侊紒錛?br />
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
#define swap(t,x,y) (t=x,x=y,y=t)
#define clr(list) memset(list,0,sizeof(list))
#define maxn 50005
using namespace std;
int father[maxn];
int num[maxn];
int find(int x)
{
if (x!=father[x])
{
int tmp=father[x];
father[x]=find(father[x]);
num[x]=(num[x]+num[tmp]) % 3;
}
return father[x];
}
void union_set(int x,int y,int d)
{
int fx=find(x);
int fy=find(y);
father[fx]=fy;
num[fx]=(num[y]-num[x]+2+d) % 3;
return ;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int ans=0;
int flag=0;
for (int i=1; i<=n; i++)
father[i]=i,num[i]=0;
for (int i=1; i<=m; i++)
{
int d,x,y;
scanf("%d%d%d",&d,&x,&y);
if (x>n || y>n)
{
ans++;
continue;
}
if (d==2 && x==y)
{
ans++;
continue;
}
int fx=find(x);
int fy=find(y);
if (d==2 && fx==fy)
{
if ((num[x]-num[y]+3)%3!=1)
ans++;
continue;
}
if (d==1 && fx==fy && num[x]!=num[y])
{
ans++;
continue;
}
union_set(x,y,d);
}
printf("%d\n",ans);
return 0;
}
]]>
{
if (tree[x].parent != x)
{
tree[x].parent = Find(tree[x].parent);
}
return tree[x].parent;
}
{
if (tree[q].depth > tree[p].depth) tree[p].parent = q;
else
{
tree[q].parent = p;
if (tree[p].depth == tree[q].depth) tree[p].depth++;
}
}
鍏朵腑Find()鍑芥暟鐢ㄤ簡璺緞鍘嬬緝浼樺寲錛岃孧erge()鍑芥暟鐢ㄤ簡鍚彂寮忓悎騫剁殑浼樺寲(涓漢鎰熻鏈変簡璺緞鍘嬬緝錛屽惎鍙戝紡鍚堝茍浼樺寲鐨勬晥鏋滃茍涓嶆槑鏄撅紝鑰岀粡甯稿洜涓洪鐩拰浠g爜鐨勯檺鍒訛紝鍚彂寮忓悎騫朵細琚垜浠渷鐣?銆?/div>
鏈変釜闂錛屽浣曟眰鑺傜偣鍒拌窡鑺傜偣鐨勮窛紱伙紵
]]>
綰挎鏁扮殑鏋勯狅紝鏌ヨ錛屼慨鏀癸紒
struct node{
int l,r,m;
long long sum;//褰撳墠鍖洪棿鐨勫拰
long long add;//鍖洪棿闇瑕佸姞涓婄殑鏁板?br />}
棰樻剰錛?br /> 緇欏畾n涓暟瀛桝1,A2,..,An錛孮涓搷浣溿?br /> C x y z,鎶婂尯闂磝 y鍐呯殑鏁伴兘鍔犱笂z
Q x y錛屾眰鍖洪棿x y鍐呮墍鏈夋暟鐨勫拰銆?br />鎬葷粨錛?br /> 鍩虹浠g爜涓瀹氳瀵癸紝璋冭瘯浜嗗崐澶╋紝鏈鍚庡彂鐜版槸swap(x,y,t)鍐欓敊浜嗭紒錛侊紒錛?br /> WA浜嗕竴嬈★紝鏁版嵁鏄痩ong long鎵嶈錛岃鍒嗘瀽娓呮鏁版嵁鑼冨洿錛侊紒錛?br />
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
#define swap(x,y,t) (t=x,x=y,y=t)
#define clr(list) memset(list,0,sizeof(list))
#define LL long long
#define maxn 200005
using namespace std;
struct node{
int l,r,m;
LL sum;
LL add;
} tree[2*maxn];
int a[maxn];
LL build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
tree[root].m=(l+r)/2;
tree[root].add=0;
if (l==r)
{
tree[root].sum=a[l];
return tree[root].sum;
}
long long sum_l=build(l,(l+r)/2,root*2);
long long sum_r=build((l+r)/2+1,r,root*2+1);
tree[root].sum=sum_l+sum_r;
return tree[root].sum;
}
int insert(int l,int r,int add,int root)
{
if (l<=tree[root].l && r>=tree[root].r)
{
tree[root].add+=add;
tree[root].sum+=add * (tree[root].r-tree[root].l+1);
return 0;
}
if (tree[root].add) //鍚戜笅鏇存柊
{
tree[2*root].sum+=tree[root].add * (tree[root*2].r-tree[root*2].l+1);
tree[2*root].add+=tree[root].add;
tree[2*root+1].sum+=tree[root].add * (tree[root*2+1].r-tree[root*2+1].l+1);
tree[2*root+1].add+=tree[root].add;
tree[root].add=0;
}
if (l<=tree[root].m)
insert(l,r,add,root*2);
if (r>tree[root].m)
insert(l,r,add,root*2+1);
tree[root].sum=tree[root*2].sum+tree[root*2+1].sum; //榪欓噷鍥炴函鍟?/span>
return 0;
}
LL search(int l,int r,int root)
{
if (l<=tree[root].l && r>=tree[root].r)
return tree[root].sum;
if (tree[root].add) //鍚戜笅鏇存柊
{
tree[2*root].sum+=tree[root].add * (tree[root*2].r-tree[root*2].l+1);
tree[2*root].add+=tree[root].add;
tree[2*root+1].sum+=tree[root].add * (tree[root*2+1].r-tree[root*2+1].l+1);
tree[2*root+1].add+=tree[root].add;
tree[root].add=0;
}
LL sum_l=0,sum_r=0;
if (l<=tree[root].m)
sum_l=search(l,r,2*root);
if (r>tree[root].m)
sum_r=search(l,r,2*root+1);
return sum_l+sum_r;
}
int main()
{
int n,m;
int x,y,z;
int tmp;
char ch;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,n,1);
for (int i=1;i<=m;i++)
{
scanf("\n%c",&ch);
if (ch=='C')
{
scanf("%d%d%d",&x,&y,&z);
if (x>y)
swap(x,y,tmp);
insert(x,y,z,1);
}
else
{
scanf("%d%d",&x,&y);
if (x>y)
swap(x,y,tmp);
printf("%I64d\n",search(x,y,1));
}
}
return 0;
}
]]>
綆楁硶錛屾暟鎹粨鏋勶紝C++錛孲TL銆傞渶瑕佸幓瀛︾殑鍟婏紝涓嬪崍寮濮嬪鍚у鍚?br />
鍥劇殑鎿嶄綔錛屼嬌鐢ㄩ泦鍚堟潵緇存姢錛屾妸鎿嶄綔搴忓垪鍊掕繃鏉ョ湅錛屽壊杈瑰垯鐪嬪仛涓や釜闆嗗悎鐨勫悎騫躲?br />
STL 鐨剆et鍜宮ultiset 寰堝己澶у晩錛岀涓鐢⊿TL鍐欎笢瑗?br />
wangs ,浣犺鍔姏浜嗭紝澶х墰涔嬭礬錛岃繙鐫鍛€?br />
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <cmath>
#include <climits>
using namespace std;
typedef pair<int, int> PII;
const int MAXN = 20010, MAXM = 60010, MAXQ = 300010;
int value[MAXN], query[MAXQ][3];
multiset<PII> e;
multiset<int> v[MAXN];
int pre[MAXN];
int root(int a)
{
if(pre[a] != a)
pre[a] = root(pre[a]);
return pre[a];
}
int unionSet(int a, int b)
{
multiset<int>::iterator it;
a = root(a), b = root(b);
if(a == b)
return 0;
if(v[a].size() > v[b].size())
swap(a, b);
for (it = v[a].begin(); it != v[a].end(); it++)
v[b].insert(*it);
v[a].clear();
pre[a] = b;
return 0;
}
int update(int a, int x)
{
int ta = a;
a = root(a);
v[a].erase(v[a].find(value[ta]));
v[a].insert(x);
value[ta] = x;
return 0;
}
int main()
{
int n, m, q;
int a, b;
int i,u;
int cas=0;
char ch[3];
double ct = 0, sum = 0;
multiset<PII>::iterator it;
multiset<int>::iterator tp;
while(scanf("%d%d%d",&n,&m,&q)==3)
{
e.clear();
for (i = 1; i <= n; i++)
v[i].clear();
for (i = 1; i <= n; i++)
{
scanf("%d", &value[i]);
pre[i] = i;
}
for (i = 0; i < m; i++)
{
scanf("%d%d", &a, &b);
if(a > b)
swap(a, b);
e.insert(PII(a, b));
}
for (i = 0; i < q; i++)
{
scanf("%s", &ch);
scanf("%d%d", &query[i][1], &query[i][2]);
query[i][0] = ch[0];
if(ch[0] == 'E')
{
if(query[i][1] > query[i][2])
swap(query[i][1], query[i][2]);
it = e.find(PII(query[i][1], query[i][2]));
e.erase(it);
}
else if(ch[0] == 'U')
swap(value[query[i][1]], query[i][2]);
}
for (i = 1; i <= n; i++)
v[i].insert(value[i]);
for (it = e.begin(); it != e.end(); it++)
unionSet(it->first, it->second);
// for (it = e.begin(); it != e.end(); it++)
// printf("%d %d\n",it->first, it->second);
ct=0;
sum=0;
for(int i = q - 1; i >= 0; i--)
{
if(query[i][0] == 'E')
unionSet(query[i][1], query[i][2]);
else if(query[i][0] == 'U')
update(query[i][1], query[i][2]);
else
{
ct += 1;
u = root(query[i][1]);
tp = v[u].lower_bound(query[i][2]);
if(tp != v[u].end())
sum += *tp;
// printf("%d %d\n",u,*tp);
}
}
printf("Case %d: %0.3lf\n",++cas,(double)sum/ct);
}
return 0;
}
]]>
windows,鎵懼嚭鍖洪棿鐨勬渶澶ф渶灝忓厓绱狅紝鍗曡皟闃熷垪鐨勭粡鍏稿簲鐢ㄥ槢銆?br />搴旇璇達紝璐績鐨勬濇兂錛屾柟娉曪紝鍗曡皟闃熷垪鏄疄鐜扮殑鎶鏈墠濂藉惂銆?br />poj2832
涓嬈C錛岃澶氱悊瑙i棶棰樼殑鏈川鍟婏紒錛侊紒
#include<string.h>
#include<math.h>
int n,k;
int a[1000005],q[1000005];
int main()
{
int i,head,tail;
printf("%d\n",(30<<1)*3);
while (scanf("%d%d",&n,&k)==2)
{
for (i=1; i<=n ; i++)
scanf("%d",&a[i]);
head=1;
tail=0;
memset(q,0,sizeof(q));
for (i=1; i<=n ; i++ )
{
while (head<=tail&&a[q[tail]]>=a[i])
tail--;
tail++;
q[tail]=i;
while (q[head]<=i-k)
head++;
if (i>=k)
printf("%d ",a[q[head]]);
}
printf("\n");
head=1;
tail=0;
memset(q,0,sizeof(q));
for (i=1; i<=n ; i++ )
{
while (head<=tail&&a[q[tail]]<=a[i])
tail--;
tail++;
q[tail]=i;
while (q[head]<=i-k)
head++;
if (i>=k)
printf("%d ",a[q[head]]);
}
printf("\n");
}
return 0;
}
n,k,i,head,tail:longint;
a,q:array[0..1000001] of longint;
begin
readln(n,k);
for i:=1 to n do
read(a[i]);
head:=1;tail:=0;
for i:=1 to n do
begin
while (head<=tail) and (a[q[tail]]>=a[i]) do dec(tail);
inc(tail);
q[tail]:=i;
while q[head]<=i-k do inc(head);
if i>=k then write(a[q[head]],' ');
end;
writeln;
fillchar(q,sizeof(q),0);
head:=1;tail:=0;
for i:=1 to n do
begin
while (head<=tail) and (a[q[tail]]<=a[i]) do dec(tail);
inc(tail);
q[tail]:=i;
while q[head]<=i-k do inc(head);
if i>=k then write(a[q[head]],' ');
end;
writeln;
end.
]]>
榪欎釜棰樼洰姣旇緝鍧戠埞錛岀畝鍗曠殑闂璁╀粬緇欏彊榪扮殑娣卞ゥ鏃犳瘮錛侊籍錛幫紒錛侊紒錛佹暟瀛︾悊瑙h兘鍔涘綋鐒墮噸瑕佸晩錛?br />銆銆鎼炴竻棰樻剰鍚庯紝鎯沖埌鐨勫氨鏄激錛頒簡錛屾劅瑙夋湁涓洓杈瑰艦涓嶇瓑寮忓湪閲岄潰鍙互鐢ㄣ傘傘傝椽蹇冩硶搴旇涔熸槸姝g‘鐨勫晩錛屼笉榪囦竴鐩達擠錛★紝涔熶笉鐭ラ亾鎬庝箞鍥炰簨銆傝濂藉ソ鐮旂┒鐮旂┒鍗曡皟鏍堬紝鍗曡皟闃熷垪浜嗐傘傘傘傚憸鍛滃憸鍖?br />銆銆錛鳳肌浜嗘棤鏁版錛屽崟璋冩爤涓嬈★肌錛o紒
#include<string.h>
#include<math.h>
int n;
long long stack[50005],pos[50005],sum;
long long ans,w,h;
int main()
{
int i,top;
while (scanf("%d",&n)==1&&n!=-1)
{
sum=0;top=0;ans=0;
for (i=1;i<=n ;i++ )
{
scanf("%I64d%I64d",&w,&h);
while (top>0&&h<=stack[top])
{
if (ans<(sum-pos[top-1])*stack[top])
ans=(sum-pos[top-1])*stack[top];
top--;
}
sum+=w;
top++;
stack[top]=h;
pos[top]=sum;
}
while (top>0)
{
if (ans<(sum-pos[top-1])*stack[top])
ans=(sum-pos[top-1])*stack[top];
top--;
}
printf("%I64d\n",ans);
}
return 0;
}
]]>
榪欎釜棰樼洰錛鳳肌浜嗕竴涓嬪崍浜嗭紝鍝庡搸錛屽緱鐮旂┒鐮旂┒鍗曡皟鏍堝拰鍗曡皟闃熷垪鍘伙紒
鎬葷粨錛氬鍙戠幇闂鐨勬湰璐ㄣ?br />鏁板鏈川錛侊紒錛?br />
#include<string.h>
#include<math.h>
long long stack[80008];
int main()
{
int i,n,top;
long long x,ans;
while (scanf("%d",&n)==1)
{
top=0;ans=0;
for (i=1; i<=n ; i++ )
{
scanf("%I64d",&x);
while (top&&x>=stack[top])
top--;
ans+=top;
top++;
stack[top]=x;
}
printf("%I64d\n",ans);
}
return 0;
}
]]>
#include<string.h>
#include<math.h>
int n,m;
int a[2005],stack[2005],pos[2005],ans;
int GetAns()
{
int i,top;
top=0;
stack[0]=pos[0]=0;
for (i=1; i<=n ; i++ )
{
while (top&&a[i]<=stack[top])
{
if (ans<(i-1-pos[top-1])*stack[top])
ans=(i-1-pos[top-1])*stack[top];
top--;
}
top++;
stack[top]=a[i];
pos[top]=i;
}
while (top)
{
if (ans<(n-pos[top-1])*stack[top])
ans=(n-pos[top-1])*stack[top];
top--;
}
return 0;
}
int main()
{
int i,j,x;
while (scanf("%d%d",&m,&n)==2)
{
memset(a,0,sizeof(a));
ans=0;
for (i=1; i<=m ; i++ )
{
for (j=1; j<=n ; j++ )
{
scanf("%d",&x);
if (x)
a[j]++;
else
a[j]=0;
}
GetAns();
}
printf("%d\n",ans);
}
return 0;
}
sdu鐨勬紲炶鐨刾oj3050璇ョ湅鐪嬩簡錛?img src ="http://www.shnenglu.com/ArcTan/aggbug/172625.html" width = "1" height = "1" />
]]>
榪欎釜涓嶆槸special judge涔堬紵
鎬葷粨錛氫互鍚庤棰樿璇繪竻妤氾紝鐞嗚В鏄庣櫧錛屼笉鑳芥湁姝т箟鍟婏紒
#include<string.h>
#include<math.h>
int pos[100005],n;
long long stack[100005],sum[100005];
int main()
{
int i,s,t,top;
long long a,ans;
while (scanf("%d",&n)==1)
{
top=0;stack[0]=0;pos[0]=0;sum[0]=0;
ans=0;s=t=0;
for (i=1;i<=n ;i++ )
{
scanf("%I64d",&a);
sum[i]=sum[i-1]+a;
while (top&&a<=stack[top])
{
if (ans<(sum[i-1]-sum[pos[top-1]])*stack[top])
{
ans=(sum[i-1]-sum[pos[top-1]])*stack[top];
s=pos[top-1]+1;t=i-1;
}
top--;
}
top++;
stack[top]=a;
pos[top]=i;
}
while (top)
{
if (ans<=(sum[n]-sum[pos[top-1]])*stack[top]) //榪欓噷鏄?lt;=鎵嶈繃鐨勶紝涓嶆槸special judge涔堬紵鎬庝箞鍥炰簨鍛紝寰楀ソ濂界湅鎳傞鐩晩錛?/span>
{
ans=(sum[n]-sum[pos[top-1]])*stack[top];
s=pos[top-1]+1;t=n;
}
top--;
}
printf("%I64d\n%d %d\n",ans,s,t);
}
return 0;
}
紿佺劧鍙戠幇鑷繁鍠滄鍐欏崟璋冩爤浜嗭紝鍛靛懙錛岃繖涓笓棰樺ソ涓滆タ銆?br />鎰熻阿sdu鐨勫濂囩鎻愪緵緇冧範涓撻錛侊紒錛丱RZ
]]>
#include<string.h>
#include<math.h>
int n,m,ans;
int a[1005],stack[1005],pos[1005];
int GetAns()
{
int i,top;
top=0;
stack[0]=pos[0]=0;
for (i=1;i<=n ;i++ )
{
while (top>0&&a[i]<=stack[top])
{
if (ans<(i-1-pos[top-1])*stack[top])
ans=(i-1-pos[top-1])*stack[top];
top--;
}
top++;
stack[top]=a[i];
pos[top]=i;
}
while (top>0)
{
if (ans<(n-pos[top-1])*stack[top])
ans=(n-pos[top-1])*stack[top];
top--;
}
}
int main()
{
int cas,i,j;
char ch[2];
scanf("%d",&cas);
while (cas--)
{
memset(a,0,sizeof(a));
ans=0;
scanf("%d%d",&m,&n);
for (i=0;i<m ;i++ )
{
for (j=1;j<=n ;j++ )
{
scanf("%s",&ch);
if (ch[0]=='F')
a[j]++;
else
a[j]=0;
}
GetAns();
}
printf("%d\n",3*ans);
}
return 0;
}
]]>
poj 2559
鎬葷粨錛歭ong long綾誨瀷錛岃鍜宨nt鍖哄垎寮鏉ワ紝杈撳嚭鏍煎紡鐢?%I64d"銆?br /> 鏁版嵁緇撴瀯璇ョ湅鐪嬩簡銆?br /> 鑷繁澶氬垎鏋愬垎鏋愩?br />鍐欏畬涓嬈″氨AC浜嗭紝涔嬪墠鐨勮椽蹇冧竴鐩撮兘WA鍛€傚洤……
#include<string.h>
#include<math.h>
int n,pos[100005];
long long stack[100005];
int main()
{
int i,top;
long long a,ans;
while (scanf("%d",&n)==1&&n)
{
top=0;ans=0;
stack[top]=0;pos[top]=0;
for (i=1 ;i<=n ;i++ )
{
scanf("%I64d",&a);
while (top>0&&a<=stack[top])
{
if (ans<(long long)(i-1-pos[top-1])*stack[top])
ans=(long long)(i-1-pos[top-1])*stack[top];
top--;
}
top++;
stack[top]=a;
pos[top]=i;
}
while (top>0)
{
if (ans<(long long)(n-pos[top-1])*stack[top])
ans=(long long)(n-pos[top-1])*stack[top];
top--;
}
printf("%I64d\n",ans);
}
return 0;
}
poj 2082
榪欎釜棰樼洰姣旇緝鍧戠埞錛岀畝鍗曠殑闂璁╀粬緇欏彊榪扮殑娣卞ゥ鏃犳瘮錛侊籍錛幫紒錛侊紒錛佹暟瀛︾悊瑙h兘鍔涘綋鐒墮噸瑕佸晩錛?br />銆銆鎼炴竻棰樻剰鍚庯紝鎯沖埌鐨勫氨鏄激錛頒簡錛屾劅瑙夋湁涓洓杈瑰艦涓嶇瓑寮忓湪閲岄潰鍙互鐢ㄣ傘傘傝椽蹇冩硶搴旇涔熸槸姝g‘鐨勫晩錛屼笉榪囦竴鐩達擠錛★紝涔熶笉鐭ラ亾鎬庝箞鍥炰簨銆傝濂藉ソ鐮旂┒鐮旂┒鍗曡皟鏍堬紝鍗曡皟闃熷垪浜嗐傘傘傘傚憸鍛滃憸鍖?br />銆銆錛鳳肌浜嗘棤鏁版錛屽崟璋冩爤涓嬈★肌錛o紒
#include<string.h>
#include<math.h>
int n;
long long stack[50005],pos[50005],sum;
long long ans,w,h;
int main()
{
int i,top;
while (scanf("%d",&n)==1&&n!=-1)
{
sum=0;top=0;ans=0;
for (i=1;i<=n ;i++ )
{
scanf("%I64d%I64d",&w,&h);
while (top>0&&h<=stack[top])
{
if (ans<(sum-pos[top-1])*stack[top])
ans=(sum-pos[top-1])*stack[top];
top--;
}
sum+=w;
top++;
stack[top]=h;
pos[top]=sum;
}
while (top>0)
{
if (ans<(sum-pos[top-1])*stack[top])
ans=(sum-pos[top-1])*stack[top];
top--;
}
printf("%I64d\n",ans);
}
return 0;
}
棰濓紝瑕佸鐨勪笢瑗胯繕寰堝鍟娿?br />
]]>