/ Vijos / 讨论 / 求和 /

怎么优化求大神解?40分其它TLE

var
n,m,i,ans,k:longint;
a:array[0..100001,1..2]of longint;
procedure dfs;
var
i,x,y,z:longint;
begin
for i:=1 to n-2 do begin
k:=0;
while i+k*2<=n do begin
inc(k);
x:=i;
y:=i+k;
z:=i+k*2;
if (a[x,2]=a[z,2]) and (x<>0) then begin
ans:=(ans+(x+z)*(a[x,1]+a[z,1])) mod 10007;
end
end;
end;
end;
begin
readln(n,m);
for i:=1 to n do read(a[i,1]);
for i:=1 to n do read(a[i,2]);
dfs;
writeln(ans);
end.

2 条评论

  • @ 2016-11-17 21:32:30

    40分,过不了。哪位大神指教一下!
    #include <iostream>
    using namespace std;

    typedef struct
    {
    int number;
    int color;
    } paper;

    int main()
    {
    ios::sync_with_stdio(false);
    int n, m;
    cin>>n>>m;
    paper a[n];
    for (int i=0; i<n; i++)
    cin>>a[i].number;
    for (int i=0; i<n; i++)
    cin>>a[i].color;
    int sum = 0;
    for (int x=0; x<n-2; x++)
    for (int y=x+1; 2*y-x<n; y++)
    {
    int z = 2 * y - x;
    if (a[x].color == a[z].color)
    {
    sum += (x+z+2) * (a[x].number+a[z].number);
    sum %= 10007;
    }
    }
    cout<<sum<<endl;

    return 0;
    }

  • @ 2016-06-10 21:15:55

    代码

    var n,m,i,sum:longint;
    num,color,t,tc,tn,tm:array[1..100000]of Int64;

    begin
    read(n,m);
    for i:=1 to n do
    read(num[i]);
    for i:=1 to n do
    read(color[i]);
    fillchar(t,sizeof(t),0);
    fillchar(tc,sizeof(tc),0);
    fillchar(tn,sizeof(tn),0);
    fillchar(tm,sizeof(tm),0);
    sum:=0;
    i:=1;
    while i<=n do
    begin
    inc(t[color[i]]);
    inc(tc[color[i]],i*num[i]);
    inc(tn[color[i]],num[i]);
    inc(tm[color[i]],i);
    inc(i,2);
    end;
    for i:=1 to m do
    if t[i]>1 then
    sum:=(sum+(t[i]-2)*tc[i]+tn[i]*tm[i])mod 10007;
    fillchar(t,sizeof(t),0);
    fillchar(tc,sizeof(tc),0);
    fillchar(tn,sizeof(tn),0);
    fillchar(tm,sizeof(tm),0);
    i:=2;
    while i<=n do
    begin
    inc(t[color[i]]);
    inc(tc[color[i]],i*num[i]);
    inc(tn[color[i]],num[i]);
    inc(tm[color[i]],i);
    inc(i,2);
    end;
    for i:=1 to m do
    if t[i]>1 then
    sum:=(sum+(t[i]-2)*tc[i]+tn[i]*tm[i])mod 10007;
    writeln(sum);
    end.

  • 1

信息

ID
1976
难度
8
分类
(无)
标签
递交数
3003
已通过
400
通过率
13%
被复制
16
上传者