thht (Thành viên)
Super fast coder
Bài viết: 58
|
nhờ moi người giúp đỡ lênh memset() trong C/C++ 8 năm, 4 tháng trước
|
(+0)
|
mình đang làm bài http://vn.spoj.pl/problems/LEM5/ trên VOJ.
2 code dưới đây giống hêt nhau, chỉ khác nhau 1 code dùng for để gán giá tri mảng f bằng 0, còn 1 code dùng memset() để khỏi tao giá trị mảng f.
Code dùng for thì được 90,91 điểm, còn code dùng memset() thì được 72.73 điểm.
Ban nào hiểu rõ lênh memset() có thể chỉ giúp mình đươc k?
code dùng for
Code: |
#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ForU(i,a,b) for(int i=a;i<=b;i++)
#define ForD(i,a,b) for(int i=a;i>=b;i--)
int const test=0;
int const nmax=100009;
int a[nmax], b[nmax], save[nmax],
f[nmax], rr[nmax], next[nmax], last[nmax];
int n,m;
int find(int x){
if (x==save[1]) return 1;
if (x==save[m]) return m;
int l=1, r=m, t=0;
while (r-l>1) {
t=(l+r)/2;
if (save[t]>x) r=t;
else l=t;
}
if (save[l]==x) return l;
if (save[r]==x) return r;
return n+1;
}
int sort(int l, int r){
int i=l, j=r, key=b[(l+r)/2], temp;
while (i<=j){
while (b[i]<key) i++;
while (b[j]>key) j--;
if (i<=j) {
temp=b[i]; b[i]=b[j]; b[j]=temp;
i++; j--;
}
}
if (l<j) sort(l,j);
if (i<r) sort(i,r);
return 0;
}
int roirac(){
ForU( i,1,n) b[i]=a[i];
sort(1,n);
m=0;
ForU( i,1,n)
if (b[i]!=b[i-1]) {
m++;
save[m]=b[i];
}
ForU( i,1,n) rr[i]=find(a[i]);
return 0;
}
int main(){
if (test) {
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
scanf("%d",&n);
ForU( i,1,n) scanf("%d",&a[i]);
roirac();
int res=0,j=0;
ForU( d,1,100) {
ForU( i,1,m+1) last[i]=n+1;last[n+1]=n+1;
ForD( i,n,1) {
j=find(a[i]+d);
next[i]=last[j];
last[rr[i]]=i;
f[i]=0;
}
ForU( i,1,n){
f[i]+=1;
if (res<f[i]) res=f[i];
if (f[next[i]]<f[i]) f[next[i]] = f[i];
}
}
cout<<res;
return 0;
}
|
code dùng memset()
Code: |
#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ForU(i,a,b) for(int i=a;i<=b;i++)
#define ForD(i,a,b) for(int i=a;i>=b;i--)
int const test=0;
int const nmax=100009;
int a[nmax], b[nmax], save[nmax],
f[nmax], rr[nmax], next[nmax], last[nmax];
int n,m;
int find(int x){
if (x==save[1]) return 1;
if (x==save[m]) return m;
int l=1, r=m, t=0;
while (r-l>1) {
t=(l+r)/2;
if (save[t]>x) r=t;
else l=t;
}
if (save[l]==x) return l;
if (save[r]==x) return r;
return n+1;
}
int sort(int l, int r){
int i=l, j=r, key=b[(l+r)/2], temp;
while (i<=j){
while (b[i]<key) i++;
while (b[j]>key) j--;
if (i<=j) {
temp=b[i]; b[i]=b[j]; b[j]=temp;
i++; j--;
}
}
if (l<j) sort(l,j);
if (i<r) sort(i,r);
return 0;
}
int roirac(){
ForU( i,1,n) b[i]=a[i];
sort(1,n);
m=0;
ForU( i,1,n)
if (b[i]!=b[i-1]) {
m++;
save[m]=b[i];
}
ForU( i,1,n) rr[i]=find(a[i]);
return 0;
}
int main(){
if (test) {
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
scanf("%d",&n);
ForU( i,1,n) scanf("%d",&a[i]);
roirac();
int res=0,j=0;
ForU( d,1,100) {
ForU( i,1,m+1) last[i]=n+1;last[n+1]=n+1;
memset(f,0,sizeof(int)*(n+5));
ForD( i,n,1) {
j=find(a[i]+d);
next[i]=last[j];
last[rr[i]]=i;
//f[i]=0;
}
ForU( i,1,n){
f[i]+=1;
if (res<f[i]) res=f[i];
if (f[next[i]]<f[i]) f[next[i]] = f[i];
}
}
cout<<res;
return 0;
}
|
|
|
|
Đã lưu IP
|
|
Đã khóa chức năng gửi bài. |
mrgaubeo (Thành viên)
Super fast coder
Bài viết: 71
|
Trả lời: nhờ moi người giúp đỡ lênh memset() trong C/C++ 8 năm, 3 tháng trước
|
(+0)
|
c
|
|
|
Đã lưu IP
|
|
Đã khóa chức năng gửi bài. |
mrgaubeo (Thành viên)
Super fast coder
Bài viết: 71
|
Trả lời: nhờ moi người giúp đỡ lênh memset() trong C/C++ 8 năm, 3 tháng trước
|
(+0)
|
d
|
|
|
Đã lưu IP
|
|
Đã khóa chức năng gửi bài. |
mrgaubeo (Thành viên)
Super fast coder
Bài viết: 71
|
Trả lời: nhờ moi người giúp đỡ lênh memset() trong C/C++ 8 năm, 3 tháng trước
|
(+0)
|
e
|
|
|
Đã lưu IP
|
|
Đã khóa chức năng gửi bài. |
mrgaubeo (Thành viên)
Super fast coder
Bài viết: 71
|
Trả lời: nhờ moi người giúp đỡ lênh memset() trong C/C++ 8 năm, 3 tháng trước
|
(+0)
|
f
|
|
|
Đã lưu IP
|
|
Đã khóa chức năng gửi bài. |
mrgaubeo (Thành viên)
Super fast coder
Bài viết: 71
|
Trả lời: nhờ moi người giúp đỡ lênh memset() trong C/C++ 8 năm, 3 tháng trước
|
(+0)
|
g
|
|
|
Đã lưu IP
|
|
Đã khóa chức năng gửi bài. |
mrgaubeo (Thành viên)
Super fast coder
Bài viết: 71
|
Trả lời: nhờ moi người giúp đỡ lênh memset() trong C/C++ 8 năm, 3 tháng trước
|
(+0)
|
h
|
|
|
Đã lưu IP
|
|
Đã khóa chức năng gửi bài. |
mrgaubeo (Thành viên)
Super fast coder
Bài viết: 71
|
Trả lời: nhờ moi người giúp đỡ lênh memset() trong C/C++ 8 năm, 3 tháng trước
|
(+0)
|
i
|
|
|
Đã lưu IP
|
|
Đã khóa chức năng gửi bài. |
mrgaubeo (Thành viên)
Super fast coder
Bài viết: 71
|
Trả lời: nhờ moi người giúp đỡ lênh memset() trong C/C++ 8 năm, 3 tháng trước
|
(+0)
|
j
|
|
|
Đã lưu IP
|
|
Đã khóa chức năng gửi bài. |
mrgaubeo (Thành viên)
Super fast coder
Bài viết: 71
|
Trả lời: nhờ moi người giúp đỡ lênh memset() trong C/C++ 8 năm, 3 tháng trước
|
(+0)
|
k
|
|
|
Đã lưu IP
|
|
Đã khóa chức năng gửi bài. |
|