Note1
目录
in & out
- 读入输出优化
关闭输入输出流同步(调试时注释掉) ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); 读入大佬版 template<class T>inline void read(T &x) { x=0;int f=0;char ch=getchar(); while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x=f?-x:x; return; }//调用时直接int n; read(n);就好了 读入基础版 inline int read() { int x=0,f=1; char ch; while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return f*x; }//调用 int x=read(); 输出 inline void write(int x) { int f=0;char ch[20]; if(!x){puts("0");return;} if(x<0){putchar('-');x=-x;} while(x)ch[++f]=x%10+'0',x/=10; while(f)putchar(ch[f--]); putchar('\n'); }//调用 write(x);
- 文件输入输出
freopen("xxx.in","r",stdin); freopen("xxx.out","w",stdout); ... fclose(stdin);fclose(stdout);
- 输入到行末、文末
输入n行,每行数量不定 for(int i=1;i<=n;i++){ char c=' '; while(c!='\n'){ cin>>a[tot++]; c=getchar(); } } 到文件结束 while(scanf("%d",&a)==1) //scanf返回值=输入元素数 或者 while(cin>>a){} 或者 while(~scanf("%d",&a))
字符数组&字符串(用法)
- 字符数组
赋值 strcpy(s1,s2); 长度 strlen(s); 比较 strcmp(s1,s2); 连接 strcat(s1,s2); 查找字串 strstr(s1,s2); //返回s2在s1中的位置,找不到则返回NULL
- 字符串(字符串排序用string)
<,>, ==号都已重载,可以直接使用(字典序) 添加 s+='a'; 长度 s.length();
数据结构
· 单调栈
例题 我的代码 #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> using namespace std; #define ll long long #define M 81000 struct node{ int v,x; }t[M]; int a[M],n,cnt,pre[M],now; ll ans; int main(){ // freopen("now.in","r",stdin); cin>>n; for(int i=1;i<=n;i++)scanf("%d",&a[i]); t[++cnt].v=a[1];t[cnt].x=1;pre[1]=cnt; int i=2;now=1; while(i<=n){ while(a[i]>=t[cnt].v&&cnt>=1){ ans+=now-pre[t[cnt].x]; cnt--; } t[++cnt].v=a[i]; pre[i]=++now; t[cnt].x=i++; } while(cnt){ ans+=now-pre[t[cnt--].x]; } cout<<ans; // fclose(stdin); }
· 单调队列
例题1 我的代码 #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> using namespace std; #define ll long long #define M 1010000 struct node{ int v,id; }q[M]; int h,t,n,k,a[M],ans[M],cnt; int main(){ // freopen("testdata.in","r",stdin); cin>>n>>k; for(int i=1;i<=n;i++)scanf("%d",&a[i]); h=1;t=0; //小 for(int i=1;i<=n;i++){ while(h<=t&&i-q[h].id+1>k)h++; while(h<=t&&a[i]<=q[h].v)h++; while(h<=t&&a[i]<=q[t].v)t--; q[++t].v=a[i];q[t].id=i; if(i>=k)ans[++cnt]=q[h].v; } for(int i=1;i<=cnt;i++)cout<<ans[i]<<" ";cout<<endl; h=1;t=0;cnt=0; //大 for(int i=1;i<=n;i++){ while(i-q[h].id+1>k)h++; while(h<=t&&a[i]>=q[h].v)h++; while(h<=t&&a[i]>=q[t].v)t--; q[++t].v=a[i];q[t].id=i; if(i>=k)ans[++cnt]=q[h].v; } for(int i=1;i<=cnt;i++)cout<<ans[i]<<" "; // fclose(stdin); }
