@cww97
2017-12-30T19:01:31.000000Z
字数 3086
阅读 1064
NOIP
typedef long long LL;
const int N = 1e5 + 7;
const double EPS = 1e-6;
const int INF = 0x3f3f3f3f; // LL 0x3f3f3f3f3f3f3f3f
return 0;
%d 读入十进制整数
%x 读入十六进制整数 %X为大写十六进制
%c 读入一个字符
%s 读入一个字符串
%f 读入一个浮点数
%p 读入一个指针
%u 读入一个无符号十进制整数
%lld linux系统LL(%I64d windows系统LL,具体情况看oj)
%llu unsigned long long
scanf
一个double
数据,是%lf
,printf
一个float
或者double
都是%f
大写L,加f输出long double
。最后的f
小写和大写没影响,但是第一个 l
必须大写成L
。
printf 对齐方式
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
把题目样例敲进in.txt
可以有效提高debug效率,建议加多组数据
for (; ~scanf("%d", &n);){
// work on problem
}
这样自己造数据测的时候可以节省更多时间
struct
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
LL gcd (LL a,LL b){
return (b==0)?a:gcd(b,a%b);
}
struct fenshu{
LL fz,fm;
fenshu (){fz = 0;fm = 1;}
fenshu (LL x,LL y):fz(x),fm(y){}
fenshu operator + (const fenshu &a)const{
LL FM = fm*a.fm/gcd(fm,a.fm) ;
LL FZ = fz*FM/fm+a.fz*FM/a.fm;
return fenshu{FZ,FM};
}
fenshu operator / (const fenshu &a)const{
LL FZ = fz*a.fm;
LL FM = fm*a.fz;
LL gc = gcd(FZ,FM);
return fenshu{FZ/gc,FM/gc};
}
}a[11],b[11];
int main(){
//freopen("in.txt","r",stdin);
LL T; scanf("%I64d",&T);
for (LL n,cas=1;cas<=T;cas++){
scanf("%I64d",&n);
for (LL i=1;i<=n;i++) a[i].fz=1 ,b[i].fz=1;
for (LL i=1;i<=n;i++) scanf("%I64d",&a[i].fz);
for (LL i=1;i<=n;i++) scanf("%I64d",&b[i].fz);
fenshu ans = fenshu(0,1);
for (LL i=n;i>=1;i--){
ans = b[i]/(ans + a[i]);
}
printf("Case #%I64d: %I64d %I64d\n",cas,ans.fz,ans.fm);
}
return 0;
}
这里给一个简单的例子
矩阵,数据结构,图,之类的,后面会struct会越来越大
比如你需要把if(ch>='a' && ch<='z') 拆成一个函数叫is_alpha()
一个函数(包括int main)尽量不要超过20行,一定不要超过30行,
超过了的话不加分割线人一眼看上去是很难受的,感觉一坨坨的
刚刚想到一个,少用全局变量
写代码的顺序,写完int main(), 再往上写int main里面用到的函数
然后函数里面调用了某个函数等这个函数写完了再补,这么写思路很连贯
一气呵成,然后debug的时候还可以模快测试
#include<cstdio>
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=1e5+100;
int n,m,len,a[N],b[N];
bool repair(int start,int h){
int cnt = 0;
for (int i=start;i<=start+n;i++)
if (b[i]<h){i+=len-1;cnt++;}
return cnt <= m;
}
bool check(int h){
for (int i=1;i<=n+len;i++)b[i]=a[i];
for (int i=1;i<=len;i++)
if (repair(i,h))return 1;
return 0;
}
int main(){
freopen("fuck.in","r",stdin);
scanf("%d%d%d",&n,&m,&len);
for (int i=1;i<=n;i++){scanf("%d",&a[i]);}
for (int i=n+1;i<=n+len;i++)a[i]=a[i-n];
int L=0,R=INF,ans;
for (;L<=R;){
int mid=(L+R)>>1;
if (check(mid)){L=mid+1;ans = mid;}
else R = mid-1;
}
printf("%d\n",ans);
return 0;
}
求三种sum丢掉第k位
预处理出所有的前缀和后缀
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 1e5 + 7;
int arr[N];
int prXor[N], prAnd[N], preOr[N];
int suXor[N], suAnd[N], sufOr[N];
int main(){
//freopen("in.txt", "r", stdin);
int n, q;
for (; ~scanf("%d%d", &n, &q);){
prXor[0] = suXor[n+1] = 0;
prAnd[0] = suAnd[n+1] = ~0;
preOr[0] = sufOr[n+1] = 0;
//printf("%d\n", prAnd[0]);
for (int i = 1; i <= n; i++){
scanf("%d", &arr[i]);
prXor[i] = prXor[i-1] ^ arr[i];
prAnd[i] = prAnd[i-1] & arr[i];
preOr[i] = preOr[i-1] | arr[i];
}
for (int i = n; i >= 1; i--){
suXor[i] = suXor[i+1] ^ arr[i];
suAnd[i] = suAnd[i+1] & arr[i];
sufOr[i] = sufOr[i+1] | arr[i];
}
for (int k; q--;){
scanf("%d", &k);
int ansXor = prXor[k-1] ^ suXor[k+1];
int ansAnd = prAnd[k-1] & suAnd[k+1];
int ansOrr = preOr[k-1] | sufOr[k+1];
printf("%d %d %d\n", ansAnd, ansOrr, ansXor);
}
}
return 0;
}