1001
#include <iostream>
#include <cassert>
using namespace std;
int main()
{
int bodyHigh,chairHigh,number,picks=0,*p,i;
cin>>bodyHigh>>chairHigh>>number;
p=new int[number];
assert(p!=0);
for(i=0;i<number;++i)
{
cin>>p[i];
if(bodyHigh+chairHigh>=p[i]) ++picks;
}
cout<<picks<<endl;
return 0;
}
1002
#include <iostream>
using namespace std;
int main()
{
int **p,L,W,a,b,i,j,maxi=0,temp;
cin>>L>>W;
p=new int *[L];
for(i=0;i<L;++i) p[i]=new int[W];
for(i=0;i<L;++i)
for(j=0;j<W;++j)
{
cin>>temp;
if(i>0&&j>0)p[i][j]=p[i-1][j]+p[i][j-1]-p[i-1][j-1]+temp;//排在最前面有助于判断的迅速,这种情况最多。
else if(i>0&&j==0) p[i][j]=p[i-1][j]+temp;
else if(i==0&&j>0) p[i][j]=p[i][j-1]+temp;
else p[i][j]=temp;
}
cin>>a>>b;
for(i=0;i+a<=L;++i)
for(j=0;j+b<=W;++j)
{
if(i>0&&j>0)temp=p[i+a-1][j+b-1]-p[i+a-1][j-1]-p[i-1][j+b-1]+p[i-1][j-1];//同上,需要注意不同的情况。
else if(i>0&&j==0) temp=p[i+a-1][j+b-1]-p[i-1][j+b-1];
else if(i==0&&j>0) temp=p[i+a-1][j+b-1]-p[i+a-1][j-1];
else temp=p[i+a-1][j+b-1];
maxi=(temp>maxi)?temp:maxi;
}
cout<<maxi<<endl;
for(i=0;i<L;++i)delete []p[i];
delete []p;
return 0;
}
1003
#include <iostream>
#include <queue>
using namespace std;
struct pos
{
int x, y;
pos(int a, int b) :x(a), y(b){}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int L;
int space[100][100];
queue<pos> q;
cin >> L;
int data;
for(int i=0;i<L;++i)
for (int j = 0;j < L;++j)
{
cin >> data;
if (data == 1)
{
space[i][j] = 2;
q.push(pos(i, j));
}
else if (data == 2) space[i][j] = 1;
else space[i][j] = 0;
}
int maxV=2;
while (!q.empty())
{
pos tmp = q.front();
q.pop();
if (tmp.x - 1 >= 0 && space[tmp.x - 1][tmp.y] == 0)
{
maxV=space[tmp.x - 1][tmp.y] = space[tmp.x][tmp.y] + 1;
q.push(pos(tmp.x - 1, tmp.y));
}
if (tmp.x + 1 < L&&space[tmp.x + 1][tmp.y] == 0)
{
maxV=space[tmp.x + 1][tmp.y] = space[tmp.x][tmp.y] + 1;
q.push(pos(tmp.x + 1, tmp.y));
}
if (tmp.y - 1 >= 0 && space[tmp.x][tmp.y - 1] == 0)
{
maxV=space[tmp.x][tmp.y - 1] = space[tmp.x][tmp.y] + 1;
q.push(pos(tmp.x, tmp.y - 1));
}
if (tmp.y + 1 < L&&space[tmp.x][tmp.y + 1] == 0)
{
maxV=space[tmp.x][tmp.y + 1] = space[tmp.x][tmp.y] + 1;
q.push(pos(tmp.x, tmp.y + 1));
}
}
cout << maxV - 2 << '\n';
cin.get();
cin.get();
return 0;
}
1004
#include <iostream>
using namespace std;
int main()
{
int M,T,U,F,D,i,*p,sum=0;
char temp;
cin>>M>>T>>U>>F>>D;
p=new int[T];
for(i=0;i<T;++i)
{
cin>>temp;
if(temp=='f') p[i]=2*F;
else p[i]=U+D;
}
for(i=0;(sum+=p[i])<=M;++i);
cout<<i<<endl;
delete []p;
return 0;
}
1005
#include <iostream>
using namespace std;
//动态四维数组。
int main()
{
int ****p,n,i,j,k,x,y;
bool judge[10]={false};
bool check(int ****);
cin>>n;
p=new int***[3];
for(i=0;i<3;++i)
{
p[i]=new int**[3];
for(j=0;j<3;++j)
{
p[i][j]=new int*[3];
for(k=0;k<3;++k)
p[i][j][k]=new int [3];
}
}
for(k=0;k<n;++k)
{
for(i=0;i<3;++i)
for(x=0;x<3;++x)
for(j=0;j<3;++j)
for(y=0;y<3;++y)
cin>>p[i][j][x][y];
judge[k]=check(p);
}
for(k=0;k<n;++k)
cout<<((judge[k])?"Right":"Wrong")<<endl;
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
for(k=0;k<3;++k)
delete []p[i][j][k];
delete []p[i][j];
}
delete []p[i];
}
delete []p;
return 0;
}
bool check(int ****p)
{
bool rowCheck(int ****,int);
bool colCheck(int ****,int);
bool bloCheck(int ****,int,int);
int i,j;
for(i=0;i<9;++i)
if(!(rowCheck(p,i))) return false;
for(j=0;j<9;++j)
if(!(colCheck(p,j))) return false;
for(i=0;i<3;++i)
for(j=0;j<3;++j)
if(!(bloCheck(p,i,j))) return false;
return true;
}
bool rowCheck(int ****p,int R)
{
int num,count,j,y;
for(num=1;num<=9;++num)
{
count=0;
for(j=0;j<3;++j)
for(y=0;y<3;++y)
if (p[R/3][j][R%3][y]==num)
{
++count;
if (count>1) return false;//大于1就不用判了。
}
if(count!=1) return false;//可能为0,此时应终止判断。
}
return true;
}
bool colCheck(int ****p,int C)
{
int num,count,i,x;
for(num=1;num<=9;++num)
{
count=0;
for(i=0;i<3;++i)
for(x=0;x<3;++x)
if(p[i][C/3][x][C%3]==num)
{
++count;
if(count>1) return false;
}
if(count!=1) return false;
}
return true;
}
bool bloCheck(int ****p,int Bi,int Bj)
{
int num,count,x,y;
for(num=1;num<=9;++num)
{
count=0;
for(x=0;x<3;++x)
for(y=0;y<3;++y)
if(p[Bi][Bj][x][y]==num)
{
++count;
if(count>1) return false;
}
if(count!=1) return false;
}
return true;
}
1006
#include <iostream>
using namespace std;
//尝试线性算法。
int main()
{
int subSum(int a[], int size);
int n, *p;
cin >> n;
p = new int[n];
for (int i = 0;i < n;++i)
cin >> p[i];
int maxSum = subSum(p, n);
if (maxSum > 0)
cout << maxSum;
else
cout << "Game Over";
delete[]p;
cin.get();
cin.get();
return 0;
}
int subSum(int a[], int size)
{
int maxSum = 0, i, tempSum = 0,num=0;//num控制至少两键。
for (i = 0;i < size;++i)
{
tempSum += a[i];
++num;
if (tempSum < 0 && i + 2 < size&&tempSum <= a[i + 2])
{
tempSum = 0;
num = 0;
}//修改条件。
if (num>=2&&tempSum > maxSum)maxSum = tempSum;//num控制至少两键。
}
return maxSum;
}
#include <iostream>
#include <climits>
#define NEGMAX -INT_MAX//可用-0xffffffff;
using namespace std;
//分治法,递归实现,貌似复杂度为O(nlogn)。普通循环算法为O(n^2)。\
//1006 求和问题。P132。
int main()
{
int n,*p,i,result;
int maxSum(int *,int ,int);
cin>>n;
p=new int[n];
for(i=0;i<n;++i)cin>>p[i];
result=maxSum(p,0,n-1);
if(result>0)cout<<result;
else cout<<"Game Over";
cout<<endl;
delete []p;
return 0;
}
int maxSum(int *p,int left,int right)
{
int maxLeft,maxRight,center,LPtemp=NEGMAX,RPtemp=NEGMAX,leftPartSum=0,rightPartSum=0,i;
if(left==right) return 0; //不大于0的数不会影响最终结果。
else if(right-left==1) return (p[left]+p[right]);
center=(left+right)/2;
maxLeft=maxSum(p,left,center);
maxRight=maxSum(p,center+1,right);
for(i=center;i>=left;--i)
{
leftPartSum+=p[i];
if(leftPartSum>LPtemp) LPtemp=leftPartSum;/*这里的LPtemp以及下面的RPtemp初始化为NEGMAX非常有必要,
这样能够保证它至少为两个键,若改为0,则有可能左半部分没
有元素,而右半部分为一个,不和要求。*/
}
for(i=center+1;i<=right;++i)
{
rightPartSum+=p[i];
if(rightPartSum>RPtemp) RPtemp=rightPartSum;
}
return max(max(maxLeft,maxRight),LPtemp+RPtemp);
}
1007
#include <iostream>
#include <cstring>
using namespace std;
//该题数据较大,且需要精确,考虑为字符串。
int main()
{
char a[204],b[204],*sum;
char * sumUp(char [],char []);
cin.getline(a,204);
cin.getline(b,204);
sum=sumUp(a,b);
cout<<((sum[0]!='0')?sum:sum+1)<<endl;
delete []sum;//防止内存泄漏。
return 0;
}
char *sumUp(char a[],char b[])
{
char *p,*temp;
temp=new char[204];
int i,tempInt,dellen;
void add(char [],char *,int);
dellen=strlen(a)-strlen(b);
p=(dellen<0)?a:b;
dellen=(dellen>0)?dellen:(-dellen);
for(i=0;i<=dellen;++i)temp[i]='0'; //保留了可能的最高位。
temp[i]='\0';
strcat(temp,p);//将短的续上,相当于对位。(但对过之后任然差1),原因是保留了可能的最高位。
p=(p==a)?b:a;
for(i=0;(temp+1)[i]!='\0';++i);
--i;
add(temp+1,p,i);//temp比a,b中较长项还长1。+1修正差值。
--i;
tempInt=(temp+1)[i]+p[i]-'0'-'0';
(temp+1)[i]=tempInt%10+'0';
(temp+1)[i-2]+=tempInt/10;//操作后(temp+1)[i-2]可能不为数字字符,但起到了储存数据的作用。
for(i-=2;i>=0;--i)
add(temp+1,p,i);//整数部分操作,包括可能最高位的填补。
return temp; //注意不能返回局部变量。
}
void add(char temp[],char *p,int i)
{
int tempInt=temp[i]+p[i]-'0'-'0';
temp[i]=tempInt%10+'0';
temp[i-1]+=tempInt/10;
}
1008
#include <iostream>
using namespace std;
//我的程序。输入改进版。
int monthDays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
struct date
{
int year;
int month;
int day;
int dayForWeek;
};
int getWeek(const date &d)
{
int m=d.month,y=d.year;
if(d.month<=2)
{
m+=12;
--y;
}
int dayForWeek=(d.day+2+2*m+3*(m+1)/5-1+y+y/4-y/100+y/400)%7; /*此公式为关键破局。该公式大大缩小了时间消耗。推导详见
http://www.cnblogs.com/hanxi/archive/2012/06/12/2545828.html*/
return dayForWeek;
}
int countDays(const date& d1,const date& d2)
{
bool isLeap(int );
int count=0,month,year;
if(d1.year==d2.year)
if(d1.month==d2.month)
return (d2.day-d1.day+1);
else
{
count=monthDays[d1.month]-d1.day+1+d2.day;
for (month=d1.month+1;month<d2.month;++month)
count+=monthDays[month];
if (2>=d1.month&&2<d2.month&&isLeap(d1.year))
++count;
return count;
}
else
{
date d3={d1.year,12,31},d4={d2.year,1,1};
count=countDays(d1,d3)+countDays(d4,d2);
for(year=d1.year+1;year<d2.year;++year)
{
count+=365;
if(isLeap(year)) ++count;
}
return count;
}
}
bool isLeap(int year)
{
return ((year%4==0&&year%100!=0)||year%400==0);
}
bool betwDays(date &d1,date &d2,date &d3)//需保证d1在d2左侧。
{
if (d1.year<d3.year&&d3.year<d2.year) return true;
else if(d3.year<d1.year||d3.year>d2.year) return false;
else if(d3.year==d1.year)
if(d1.year==d2.year)
{
if(d1.month<d3.month&&d3.month<d2.month) return true;
else if(d3.month<d1.month||d3.month>d2.month) return false;
else if(d3.month==d1.month)
if(d1.month==d2.month)
if(d1.day<=d3.day&&d3.day<=d2.day)return true;
else return false;
else
if(d3.day>=d1.day) return true;
else return false;
else
if(d3.day<=d2.day)return true;
else return false;
}
else
{
date d4={d1.year,12,31};
return (betwDays(d1,d4,d3));
}
else
{
date d4={d2.year,1,1};
return (betwDays(d4,d2,d3));
}
}
void dayUp(date &d)
{
bool isLeap(int);
++d.day;
if(d.day>monthDays[d.month]+(d.month==2&&isLeap(d.year)))
{
d.day=1;
++d.month;
if(d.month>12)
{
d.month=1;
++d.year;
}
}
++d.dayForWeek;
if(d.dayForWeek>6) d.dayForWeek=0;
}
int main()
{
date d1,d2,dtemp;
char eat;
int n,*p,i,j,daysNum,holidays,year;
cin>>n;
p=new int[n];
for(i=0;i<n;++i)
{
cin>>d1.year>>eat>>d1.month>>eat>>d1.day;
cin>>d2.year>>eat>>d2.month>>eat>>d2.day;
d1.dayForWeek=getWeek(d1);
d2.dayForWeek=getWeek(d2);
daysNum=countDays(d1,d2);
holidays=daysNum/7*2;
for(dtemp=d1,j=0;j<daysNum%7;++j)
{
if(dtemp.dayForWeek==0||dtemp.dayForWeek==6)
++holidays;
dayUp(dtemp);
}
for(year=d1.year;year<=d2.year;++year)
{
dtemp.year=year;
dtemp.month=dtemp.day=1;
dtemp.dayForWeek=getWeek(dtemp);
if(year!=d1.year&&year!=d2.year||betwDays(d1,d2,dtemp))//若不为首尾年,则无需调用函数。
holidays+=(dtemp.dayForWeek!=0&&dtemp.dayForWeek!=6);
dtemp.month=5;
dtemp.day=1;
dtemp.dayForWeek=getWeek(dtemp);
for(j=0;j<3;++j)
{
if(year!=d1.year&&year!=d2.year||betwDays(d1,d2,dtemp))
holidays+=(dtemp.dayForWeek!=0&&dtemp.dayForWeek!=6);
dayUp(dtemp);
}
dtemp.month=10;
dtemp.day=1;
dtemp.dayForWeek=getWeek(dtemp);
for(j=0;j<7;++j)
{
if(year!=d1.year&&year!=d2.year||betwDays(d1,d2,dtemp))
holidays+=(dtemp.dayForWeek!=0&&dtemp.dayForWeek!=6);
dayUp(dtemp);
}
}
p[i]=daysNum-holidays;
}
for(i=0;i<n;++i)
cout<<p[i]<<endl;
delete []p;
return 0;
}
1009
#include <iostream>
#include <iomanip>
using namespace std;
struct trade
{
int t,a,d;
};
struct market
{
int t,v;
};
int main()
{
int m,n,i,j;
double solid,flexible,sum=0;
trade *p;
market *q;
cin>>m;
p=new trade[m];
for(i=0;i<m;++i)
cin>>p[i].t>>p[i].a>>p[i].d;
cin>>n;
q=new market[n];
for(i=0;i<n;++i)
cin>>q[i].t>>q[i].v;
for(i=0;i<m;++i)
{
for(j=0;j<n-1&&q[j+1].t<=p[i].t;++j);
flexible=p[i].a*100*q[j].v; //flexible现在储存的是交易额;
solid=((0.002*flexible>5)?(0.002*flexible):5)+p[i].a*100/1000.0*1+1;
solid=-solid;
if(p[i].d==1)
flexible=-flexible;
else
flexible-=0.001*flexible;//flexible现在储存的是不同的部分;
sum+=flexible+solid;
}
cout<<fixed<<setprecision(2)<<sum<<endl;
delete []p;
delete []q;
return 0;
}
1010
#include <iostream>
using namespace std;
const int salary=300;
int main()
{
int broEr=0,mother=0,i,budget;
for(i=1;i<=12;++i)
{
broEr+=salary;
cin>>budget;
if(broEr<budget){cout<<'-'<<i<<endl;break;}
mother+=(broEr-budget)/100*100;
broEr=(broEr-budget)%100;
}
if(i==13)cout<<broEr+mother*1.2<<endl;
return 0;
}
1011
#include <iostream>
#include <iomanip>
using namespace std;
class MyComplex
{
friend istream &operator>>(istream &is,MyComplex &obj);
friend ostream &operator<<(ostream &os,const MyComplex &obj);
friend MyComplex operator+(const MyComplex &obj1,const MyComplex &obj2);
friend MyComplex operator-(const MyComplex &obj1,const MyComplex &obj2);
friend MyComplex operator*(const MyComplex &obj1,const MyComplex &obj2);
friend MyComplex operator/(const MyComplex &obj1,const MyComplex &obj2);
private:
double x,y;
public:
MyComplex(double a=0,double b=0) :x(a),y(b){}
MyComplex& operator+=(const MyComplex &obj){*this=*this+obj;return *this;}
MyComplex& operator-=(const MyComplex &obj){*this=*this-obj;return *this;}
MyComplex& operator*=(const MyComplex &obj){*this=*this*obj;return *this;}
MyComplex& operator/=(const MyComplex &obj){*this=*this/obj;return *this;}
};
istream &operator>>(istream &is,MyComplex &obj)
{
is>>obj.x>>obj.y;
return is;
}
ostream & operator<<(ostream &os,const MyComplex &obj)
{
os<<obj.x<<' '<<obj.y;
return os;
}
MyComplex operator+(const MyComplex &obj1,const MyComplex &obj2)
{
MyComplex temp;
temp.x=obj1.x+obj2.x;
temp.y=obj1.y+obj2.y;
return temp;
}
MyComplex operator-(const MyComplex &obj1,const MyComplex &obj2)
{
MyComplex temp;
temp.x=obj1.x-obj2.x;
temp.y=obj1.y-obj2.y;
return temp;
}
MyComplex operator*(const MyComplex &obj1,const MyComplex &obj2)
{
MyComplex temp;
temp.x=obj1.x*obj2.x-obj1.y*obj2.y;
temp.y=obj1.x*obj2.y+obj1.y*obj2.x;
return temp;
}
MyComplex operator/(const MyComplex &obj1,const MyComplex &obj2)
{
MyComplex temp;
temp.x=(obj1.x*obj2.x+obj1.y*obj2.y)/(obj2.x*obj2.x+obj2.y*obj2.y);
temp.y=(obj1.y*obj2.x-obj1.x*obj2.y)/(obj2.x*obj2.x+obj2.y*obj2.y);
return temp;
}
int main()
{
MyComplex z1;
MyComplex z2;
cin >> z1 >> z2;
cout<<fixed<<setprecision(2);
cout << z1 + z2 <<endl;
cout << z1 - z2 <<endl;
cout << z1 * z2 <<endl;
cout << z1 / z2 <<endl;
cout << (z1 += z2) <<endl;
cout << (z1 -= z2) <<endl;
cout << (z1 *= z2) <<endl;
cout << (z1 /= z2) <<endl;
return 0;
}
1012
#include <iostream>
using namespace std;
struct state //前面几次错误分析数据,都是在数据很大时出错,刚才测试1,200000很大,貌似超过了int,改为long long,遂成。
{
long long length;
long long number;
state() :length(1), number(1) {}
};
int main()
{
void solve(state *, int, int);
int S, T;
cin >> S >> T;
state *a = new state[T - S + 1];
solve(a, S, T);
long long num = 0, len = 0;
for(int i=0;i<T-S+1;++i)
{
if (a[i].length > len)
{
len = a[i].length;
num = a[i].number;
}
else if (a[i].length == len) num+=a[i].number;
}
cout << len << endl;
cout << num << endl;
cin.get();
cin.get();
}
void solve(state *a, int S, int T)//直接找下个能被整除的。
{
int i, j,tmp; //i是下标,S+i是数。
for(i=0;i<T-S+1;++i)
for (j = 1;j <= 100;++j) //这个地方为什么到100?。。。
{
if ((i+S)*j % 100 == 0&&i+(S+i)*j/100<T-S+1)
{
tmp = i + (S+i)*j / 100;
if (a[tmp].length < a[i].length + 1)
{
a[tmp].length = a[i].length + 1;
a[tmp].number = a[i].number;
}
else if (a[tmp].length == a[i].length + 1)
a[tmp].number += a[i].number;
}
}
}
1013
#include <iostream>
using namespace std;
int main()
{
int c[1000], w[1000];
int state[10001] = { 0 };
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int V, N;
cin >> V >> N;
for (int i = 0;i < N;++i)
{
cin >> c[i] >> w[i];
for (int j = 1;j <= V;++j)
if (j >= c[i] && state[j - c[i]] + w[i] > state[j])state[j] = state[j - c[i]] + w[i];
}
cout << state[V] << '\n';
cin.get();
cin.get();
return 0;
}
1014
#include <iostream>
#include <list>
#include <stack>
using namespace std;
int main()
{
char c;
stack<int> num1, num2;
cin.get(c);
while (c != '\n')
{
num1.push(c - '0');
cin.get(c);
}
cin.get(c);
while (c != '\n')
{
num2.push(c - '0');
cin.get(c);
}
int carry=0;
list<int> result;
while (!num1.empty() && !num2.empty())
{
result.push_front((num1.top() + num2.top() + carry) % 10);
carry = (num1.top() + num2.top() + carry) / 10;
num1.pop();
num2.pop();
}
while (!num1.empty())
{
result.push_front((num1.top() + carry) % 10);
carry = (num1.top() + carry) / 10;
num1.pop();
}
while (!num2.empty())
{
result.push_front((num2.top() + carry) % 10);
carry = (num2.top() + carry) / 10;
num2.pop();
}
if (carry == 1) result.push_front(1);
while (!result.empty())
{
cout << result.front();
result.pop_front();
}
cout << endl;
cin.get();
return 0;
}
1015
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int main()
{
vector <int> num1, num2;
char str[1001];
int i, j;
cin >> str;
for (i = strlen(str) - 1;i >= 0;--i)num1.push_back(str[i] - '0');
cin >> str;
for (i = strlen(str) - 1;i >= 0;--i)num2.push_back(str[i] - '0');
vector <int> result(num1.size() + num2.size() - 1, 0); //初始化优化。了解构造函数。
for (i = 0;i < num1.size();++i)
for (j = 0;j < num2.size();++j)
result[i + j] += num1[i] * num2[j];
int carry = 0;
for (i = 0;i < result.size();++i)
{
int tmp = (result[i] + carry) % 10;
carry = (result[i] + carry) / 10;
result[i] = tmp;
}
if (carry >0)result.push_back(carry);
if (result.back()!=0) for (i = result.size() - 1;i >= 0;--i) cout << result[i];
else cout << 0; //输出优化。若首位为0,则根据以上算法,结果应为0。否则按位输出。
cout << endl;
cin.get();
cin.get();
return 0;
}
1016
#include <iostream>
#include <vector>
#include <queue>
#include <string>
using namespace std;
class hugeInt
{
friend hugeInt operator-(const hugeInt& n1, const hugeInt& n2);
friend string operator/(const string& n1, const hugeInt& n2);
friend bool operator<(const hugeInt& n1, const hugeInt& n2);
private:
vector <int>num;
void delZeros();
public:
hugeInt(const string str);
hugeInt() {}
};
void hugeInt::delZeros()
{
int i;
for (i = num.size() - 1;i >= 1 && num[i] == 0;--i);
queue <int> tmp;
for (int j = 0;j <= i;++j)tmp.push(num[j]);
num.clear();
for (int j = 0;j <= i;++j)
{
num.push_back(tmp.front());
tmp.pop();
}
}
hugeInt::hugeInt(const string str)
{
for (int i = str.length() - 1;i >= 0;--i)
num.push_back(str[i] - '0');
}
hugeInt operator-(const hugeInt& n1, const hugeInt& n2)
{
hugeInt tmp;
int borrow = 0,i=0;
for (i=0;i < n1.num.size() && i < n2.num.size();++i)
{
if (n1.num[i] - borrow >= n2.num[i])
{
tmp.num.push_back(n1.num[i] - borrow - n2.num[i]);
borrow = 0;
}
else
{
tmp.num.push_back(n1.num[i] - borrow + 10 - n2.num[i]);
borrow = 1;
}
}
for (;i < n1.num.size();++i)
{
if (n1.num[i] - borrow >= 0)
{
tmp.num.push_back(n1.num[i] - borrow);
borrow = 0;
}
else
{
tmp.num.push_back(n1.num[i] - borrow + 10);
borrow = 1;
}
}
for (;i < n2.num.size();++i)
{
if (n2.num[i] - borrow >= 0)
{
tmp.num.push_back(n2.num[i] - borrow);
borrow = 0;
}
else
{
tmp.num.push_back(n2.num[i] - borrow + 10);
borrow = 1;
}
}
tmp.delZeros();
return tmp;
}
bool operator<(const hugeInt& n1, const hugeInt& n2)
{
if (n1.num.size() < n2.num.size()) return true;
if (n1.num.size() == n2.num.size())
{
for (int i = n1.num.size()-1;i >= 0;--i)
if (n1.num[i] < n2.num[i]) return true;
else if (n1.num[i] > n2.num[i]) return false;
}
return false;
}
string operator/(const string& n1, const hugeInt& n2)
{
if (hugeInt(n1) < n2) return "0";
string result = "";
int calMark = n2.num.size();
string tmp = n1;
tmp.erase(n2.num.size(),n1.length()-n2.num.size());
if (hugeInt(tmp) < n2) tmp.append(1,n1[calMark++]);
hugeInt beDivided(tmp);
int oneSeat;
while (true)
{
oneSeat = 0;
while (!(beDivided < n2))
{
beDivided = beDivided - n2;
++oneSeat;
}
result.append(1, char(oneSeat + '0'));
beDivided.num.push_back(0);
for (int i = beDivided.num.size()-1;i>=1;--i)beDivided.num[i] = beDivided.num[i - 1];
if (calMark == n1.length()) break;
else beDivided.num[0] = n1[calMark++] - '0';
}
return result;
}
int main()
{
string n1, n2;
cin >> n1 >> n2;
hugeInt num2(n2);
cout << n1 / num2 << endl;
cin.get();
cin.get();
return 0;
}
1017
#include <iostream>
#include <vector>
using namespace std;
class hugeInt
{
friend hugeInt operator+(const hugeInt&n1, const hugeInt&n2)
{
hugeInt result;
result.num.clear();
int i = 0;
for (;i < n1.num.size() && i < n2.num.size();++i)result.num.push_back(n1.num[i] + n2.num[i]);
for (;i < n1.num.size();++i)result.num.push_back(n1.num[i]);
for (;i < n2.num.size();++i)result.num.push_back(n2.num[i]);
for (i = 0;i < result.num.size() - 1;++i)
{
result.num[i + 1] += result.num[i] / 10;
result.num[i] = result.num[i] % 10;
}
if (result.num[i] >= 10)
{
result.num.push_back(result.num[i] / 10);
result.num[i] = result.num[i] % 10;
}
return result;
}
friend ostream& operator<<(ostream &os, const hugeInt& n)
{
for (int i = n.num.size() - 1;i >= 0;--i)os << n.num[i];
return os;
}
friend hugeInt operator*(const hugeInt& n1, int n2)
{
hugeInt result;
result.num.clear();
int i;
for (i = 0;i < n1.num.size();++i)result.num.push_back(n1.num[i] * n2);
for (i = 0;i < n1.num.size() - 1;++i)
{
result.num[i + 1] += result.num[i] / 10;
result.num[i] = result.num[i] % 10;
}
if (result.num[i] >= 10)
{
result.num.push_back(result.num[i] / 10);
result.num[i] = result.num[i] % 10;
}
return result;
}
private:
vector<int> num;
public:
hugeInt() { num.push_back(0); }
hugeInt(int n)
{
while (n > 0)
{
num.push_back(n % 10);
n /= 10;
}
if (num.empty())num.push_back(0);
}
};
hugeInt dp[1001][4];
int main()
{
int a, b, c, N;
cin >> a >> b >> c >> N;
dp[0][0] = 1;
for (int i = 1;i <= N;++i)
{
dp[i][1] = dp[i - 1][0];
dp[i][2] = dp[i - 1][1];
dp[i][3] = dp[i - 1][2] + dp[i - 1][3];
dp[i][0] = dp[i][1] * a + dp[i][2] * b + dp[i][3] * c;
}
hugeInt ans = dp[N][0] + dp[N][1] + dp[N][2] + dp[N][3];
cout << ans << endl;
cin.get();
cin.get();
return 0;
}
1018
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a, b;
while (cin >> a >> b) cout << a + b << '\n';
return 0;
}
1019
#include <iostream>
using namespace std;
int main()
{
int cnt;
int N;
cin >> N;
char str[105];
for (int i = 0;i < N;++i)
{
cin >> str;
cnt = 0;
char *p = str;
for (;*p;++p)
{
if (*p == '(')++cnt;
else
{
if (--cnt < 0)break;
}
}
cout << (cnt == 0 ? "YES" : "NO") << endl;
}
cin.get();
cin.get();
return 0;
}
1020
#include <iostream>
#include <cmath>
using namespace std;
int ans[35];
int cnt[35] = { 0 };
int len = 0;
int main()
{
int N;
cin >> N;
int num = N;
for (int i = 2; i <= sqrt(N); ++i)
{
bool flag = false;
while (N%i == 0)
{
++cnt[len];
N /= i;
flag = true;
}
if (flag) ans[len++] = i;
}
if (N > 1)
{
++cnt[len];
ans[len++] = N;
}
cout << num << '=';
for (int i = 0; i < len; ++i)
cout << ans[i] << '(' << cnt[i] << ')';
cout << endl;
cin.get();
cin.get();
return 0;
}
1021
#include <iostream>
#include <iomanip>
using namespace std;
int arr[150][150] = { 0 };
int main()
{
int n;
cin >> n;
int j, k;
j = k = 0;
int direc = 0;
for (int i = 1;i <= n*n;++i)
{
arr[j][k] = i;
switch (direc)
{
case 0:if (k != n - 1&&!arr[j][k+1])++k;
else
{
++direc;
++j;
}
break;
case 1:if (j != n - 1&&!arr[j+1][k])++j;
else
{
++direc;
--k;
}
break;
case 2:if (k != 0&&!arr[j][k-1])--k;
else
{
++direc;
--j;
}
break;
case 3:if (j != 0&&!arr[j-1][k])--j;
else
{
direc = 0;
++k;
}
}
}
for (int i = 0;i < n;++i)
{
for (int j = 0;j < n;++j)
cout << setw(6) << arr[i][j];
cout << endl;
}
cin.get();
cin.get();
return 0;
}
1022
#include <iostream>
using namespace std;
int main()
{
int a = 1, b = 1;
long long N;
cin >> N;
N = N % 2040;
for (long long i = 3;i <= N;++i)
{
int tmp = b;
b = (a + b)%2010;
a = tmp;
}
cout << b << endl;
cin.get();
cin.get();
}
1024
#include <iostream>
using namespace std;
int main()
{
int a = 1, b = 1;
long long N;
cin >> N;
N = N % 2040;
for (long long i = 3;i <= N;++i)
{
int tmp = b;
b = (a + b)%2010;
a = tmp;
}
cout << b << endl;
cin.get();
cin.get();
}
1026
#include <iostream>
#include <cstring>
//别人的代码。可见我的工具并没有简化程序。
#define MAX 10001
using namespace std;
char A[MAX] = { '0' }, B[MAX] = { '0' };
int lenA = 0, lenB = 0;
int res[MAX] = { 0 };
bool isGreater() {
return strncmp(A, B, lenB) >= 0;//注意 lenB会因为移位而增大,所以不必担心
}
//把A当前的最高位和B的最高位对齐后相减
void sub() {
//首先要找到A的最高位 和B进行对齐,由于在while循环里已经进行了大小的判断,此时最高位指的就是真正的最高位
int i, begin = 0;
for (i = 0; A[i] == '0'; ++i)
begin = i;
//此时begin和i都是最高位的下标 已经是和B对齐的了
for (; i < lenB; ++i)
A[i] = A[i] - B[i] + '0'; //注意 如果用 -= 的话 应该是 A[i] -= B[i] - '0'
//开始借位 把负数补全 从右向左借
for (int i = lenB - 1; i >0; --i) if (A[i]<'0')
{
A[i] += 10;
A[i - 1]--;
}
}
int main(int argc, char const *argv[])
{
cin >> A >>B;
lenA = strlen(A);
lenB = strlen(B);
//若A小于B 直接输出0结束.
if (lenA<lenB||(lenA == lenB && !isGreater())) {
cout << 0 << endl;cin.get();cin.get();return 0;
}
int res_i = 0; //标记res数组正在处理的下标 0是最高位
while (1) { //当最后对齐时break
//开始对A和B的前lenB位进行循环减法
while (isGreater()) {
sub();
++res[res_i];
}
++res_i;
//若上一次的余数小于B的位数,则cot为0没有问题
if (lenA == lenB) //必须在减法循环结束之后立刻检验 因为移位之后lenB发生了变化 而且也没有必要
break;
//对B进行向右移动 注意lenB要增加1
int j;
for (j = lenB++; j > 0; --j)
B[j] = B[j - 1];
B[j] = '0';
}
bool flag = true;//表示以后有0不输出
for (int i = 0; i < res_i; ++i)
{
if (flag && res[i] != 0)
flag = false;//已经遇到了 第一个不是0的数 表示以后有0输出
if (res[i] == 0) {
if (!flag)
cout << 0;
}
else
cout << res[i];
}
cout << endl;
cin.get();
cin.get();
return 0;
}
1032
#include <iostream>
using namespace std;
int A(int m, int n)
{
if (m == 0)
return n + 1;
else if (n == 0)
return A(m - 1, 1);
else
return A(m - 1, A(m, n - 1));
}
int main()
{
int m, n;
cin >> m >> n;
cout << A(m, n) << endl;
cin.get();
cin.get();
return 0;
}
1033
#include <iostream>
#include <stack>
#include <cmath>
using namespace std;
char str[101];
enum token { OPAREN, ADD, SUB, MULTI, DIV, EXP, MINUS, CPAREN};
stack<long long> num;
stack<token> op;
class error {};
void oneStepCalc(token tk)
{
if (tk == MINUS)
{
long long tmp = num.top();
num.pop();
num.push(-tmp);
return;
}
long long num1, num2;
if (num.empty())throw error();
num2 = num.top();
num.pop();
if (num.empty())throw error();
num1 = num.top();
num.pop();
long long result;
switch (tk)
{
case ADD:result = num1 + num2;break;
case SUB:result = num1 - num2;break;
case MULTI:result = num1 * num2;break;
case DIV:if (num2 == 0)throw error();result = num1 / num2;break;
case EXP:result = pow(num1, num2);break;
}
num.push(result);
}
long long calc()
{
bool lastIsNum = false;
char *p = str;
while (*p != '\0')
{
while (*p == ' ')++p;
if (*p >= '0'&&*p <= '9')
{
int n = 0;
while (*p >= '0'&&*p <= '9')
{
n = n * 10 + *p - '0';
++p;
}
num.push(n);
lastIsNum = true;
}
else
{
switch (*p)
{
case '(':op.push(OPAREN);break;
case '+':while (!op.empty() && op.top() >= ADD)
{
token tmp = op.top();
op.pop();
oneStepCalc(tmp);
}
op.push(ADD);
lastIsNum = false;
break;
case '-':if (!lastIsNum)
op.push(MINUS);
else
{
while (!op.empty()&&op.top() >= ADD)
{
token tmp = op.top();
op.pop();
oneStepCalc(tmp);
}
op.push(SUB);
}
lastIsNum = false;
break;
case '*':case '/':while (!op.empty() && op.top() >= MULTI)
{
token tmp = op.top();
op.pop();
oneStepCalc(tmp);
}
if (*p == '*')op.push(MULTI);
else op.push(DIV);
lastIsNum = false;
break;
case '^':while (!op.empty() && op.top() == MINUS)
{
op.pop();
oneStepCalc(MINUS);
}
op.push(EXP);
lastIsNum = false;
break;
case ')':if (op.empty())throw error();
while (op.top() != OPAREN)
{
token tmp = op.top();
op.pop();
if (op.empty())throw error();
oneStepCalc(tmp);
}
op.pop();
lastIsNum = true;
break;
}
++p;
}
}
while (!op.empty())
{
token tmp = op.top();
if (tmp == OPAREN) throw error();
op.pop();
oneStepCalc(tmp);
}
return num.top();
}
int main()
{
cin.getline(str, 101);
try
{
long long result = calc();
cout << result << endl;
}
catch (error) { cout << "Error" << endl; }
cin.get();
return 0;
}
1034
#include <iostream>
using namespace std;
int q[200010], rp[400010];
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, K;
cin >> N >> K;
for (int i = 0;i < K - 1;++i)
{
cin >> rp[i];
rp[i + N] = rp[i];
}
for (int i = K - 1;i < N;++i) cin >> rp[i];
int ans = 0x7fffffff;
int front = 0, back = -1;
for (int i = 0;i < K;++i)
{
while (back >= front&&q[back] <= rp[i])--back;
q[++back] = rp[i];
}
if (ans > q[front])ans = q[front];
for (int i = K;i < N + K - 1;++i)
{
if (rp[i - K] == q[front])++front;
while (back >= front&&q[back] <= rp[i])--back;
q[++back] = rp[i];
if (ans > q[front])ans = q[front];
}
cout << ans << '\n';
cin.get();
cin.get();
return 0;
}
1036
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
long long *time = new long long[N];
int i;
for (i = 0;i < N;++i)cin >> time[i];
long long oldWait=0, oldFinish=0;
long long oldWaitOfServer[3] = { 0 };
long long oldFinishOfServer[3] = { 0 };
for (i = 0;i < 3;++i)
{
for (int j = i;j < N;j += 3)
{
oldWaitOfServer[i] += time[j] * (N/3+(i<N%3) - j / 3 - 1);
oldFinishOfServer[i] += time[j];
}
oldWait += oldWaitOfServer[i];
if (oldFinishOfServer[i] > oldFinish)oldFinish = oldFinishOfServer[i];
}
long long newServer[3] = { 0 };
i = 0;
long long newWait = 0, newFinish = 0;
while (i < N)
{
int p = 0;
if (newServer[1] < newServer[p]) p = 1;
if (newServer[2] < newServer[p]) p = 2;
long long tmp = newServer[p];
for (int j = 0;j < 3;++j) newServer[j] -= tmp;
newWait += tmp*(N - i);
newFinish += tmp;
newServer[p] = time[i++];
}
long long tmp = newServer[0];
if (newServer[1] > tmp) tmp = newServer[1];
if (newServer[2] > tmp) tmp = newServer[2];
newFinish += tmp;
cout << oldWait << ' ' << oldFinish << endl;
cout << newWait << ' ' << newFinish << endl;
delete[]time;
cin.get();
cin.get();
return 0;
}//这道题做的有点智障。想得太复杂了。贴一下别人的代码,存一下,不是抄袭。
/*
#include <iostream>
#include <stdio.h>
using namespace std;
int a[100000];
long long s[3]={0},sum[3]={0};
int minIndex()
{
int t=(sum[1]<=sum[2]?1:2);
t=(sum[0]<=sum[t]?0:t);
return t;
}
long long max(long long a,long long b,long long c)
{
b=(b>c?b:c);
return (a>b?a:b);
}
int main()
{
int n,i,k;
long long w1,t1,w2,t2;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i+=3)
{
s[0]+=sum[0];
sum[0]+=a[i];
}
for(i=1;i<n;i+=3)
{
s[1]+=sum[1];
sum[1]+=a[i];
}
for(i=2;i<n;i+=3)
{
s[2]+=sum[2];
sum[2]+=a[i];
}
w1=s[0]+s[1]+s[2];
t1=max(sum[0],sum[1],sum[2]);
s[0]=s[1]=s[2]=sum[0]=sum[1]=sum[2]=0;
for(i=0;i<n;i++)
{
k=minIndex();
s[k]+=sum[k];
sum[k]+=a[i];
}
w2=s[0]+s[1]+s[2];
t2=max(sum[0],sum[1],sum[2]);
printf("%lld %lld\n%lld %lld\n",w1,t1,w2,t2);
return 0;
}
*/
1037
#include <iostream>
using namespace std;
class AvlTree
{
private:
struct node
{
int data, cnt;
node *left, *right;
int height;
node(const int& d, node* p = NULL, node* q = NULL, int h = 0) :data(d),cnt(1),left(p), right(q), height(h) {}
};
node *root;
int max(int a, int b)
{
return (a > b ? a : b);
}
void makeEmpty(node *&t)
{
if (t)
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
t = NULL;
}
}
int height(node *t) const { return t ? t->height : -1; }
void LL(node *&t)
{
node *t1 = t->left;
t->left = t1->right;
t1->right = t;
t->height = max(height(t->left), height(t->right)) + 1;
t1->height = max(height(t1->left), height(t1->right)) + 1;
t = t1;
}
void RR(node *&t)
{
node *t1 = t->right;
t->right = t1->left;
t1->left = t;
t->height = max(height(t->left), height(t->right)) + 1;
t1->height = max(height(t1->left), height(t1->right)) + 1;
t = t1;
}
void LR(node *&t)
{
RR(t->left);
LL(t);
}
void RL(node *&t)
{
LL(t->right);
RR(t);
}
void insert(const int& x, node *&t)
{
if (!t) t = new node(x);
else if (x < t->data)
{
insert(x, t->left);
if (height(t->left) - height(t->right) == 2)
if (x < t->left->data)LL(t);else LR(t);
}
else if (x > t->data)
{
insert(x, t->right);
if (height(t->right) - height(t->left) == 2)
if (x < t->right->data)RL(t);else RR(t);
}
else ++t->cnt;
t->height = max(height(t->left), height(t->right)) + 1;
}
bool remove(const int& x, node *&t)
{
bool stop = false;
int subTree;
if (t == NULL) return true;
if (x < t->data) { stop = remove(x, t->left);subTree = 1; }
else if (x > t->data) { stop = remove(x, t->right);subTree = 2; }
else if (t->cnt>1) { --t->cnt;stop = true; }
else if (t->left != NULL&&t->right != NULL)
{
node *tmp = t->right;
while (tmp->left) tmp = tmp->left;
t->data = tmp->data;
t->cnt = tmp->cnt;
tmp->cnt = 1;
stop = remove(t->data, t->right);
subTree = 2;
}
else
{
node *old = t;
t = (t->left ? t->left : t->right);
delete old;
return false;
}
if (stop) return true;
else t->height = max(height(t->left), height(t->right)) + 1;
int bf;
switch (subTree) {
case 1:bf = height(t->left) - height(t->right) + 1;
if (bf == 0) return true;
if (bf == 1) return false;
if (bf == -1)
{
int bfr = height(t->right->left) - height(t->right->right);
switch (bfr)
{
case 0:RR(t);return true;//平衡度为0。
case -1:RR(t);return false;//平衡度相同。
default:RL(t);return false;//平衡度相反。
}
}
break;
case 2:bf = height(t->left) - height(t->right) - 1;
if (bf == 0) return true;
if (bf == -1) return false;
if (bf == 1)
{
int bfl = height(t->left->left) - height(t->left->right);
switch (bfl)
{
case 0:LL(t);return true;
case 1:LL(t);return false;
default:LR(t);return false;
}
}
}
}
int find(const int&x, node *t) const
{
if (!t) return 0x7fffffff;
if (t->data >= x) return find(x, t->left);
else
{
int xx = find(x, t->right);
if (xx != 0x7fffffff && xx > t->data)return xx;
else return t->data;
}
}
public:
AvlTree(node *t = NULL) :root(t) {}
~AvlTree() { makeEmpty(root); }
int find(const int&x)
{
return find(x, root);
}
void insert(const int& x) { insert(x, root); }
void remove(const int& x) { remove(x, root); }
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M;
cin >> N >> M;
int tmp[10005];
int dat;
AvlTree t;
for (int i = 0;i < N;++i)cin >> tmp[i];
for (int i = 0;i < M;++i)
{
cin >> dat;
t.insert(dat);
}
int ans = N;
for (int i = 0;i < N;++i)
{
int bad = t.find(tmp[i]);
if (bad != 0x7fffffff)
{
t.remove(bad);
++ans;
}
}
cout << ans << '\n';
cin.get();
cin.get();
return 0;
}
1038
#include <iostream>
using namespace std;
class circle
{
private:
struct monkey
{
int num;
monkey *next, *prev;
monkey(int n, monkey* p = NULL, monkey*q = NULL) :num(n), next(p), prev(q) {}
}*currentP;
public:
circle(int n)
{
currentP = new monkey(1);
monkey *tmp = currentP;
for (int i = 2;i <= n;++i)
{
tmp = tmp->next = new monkey(i, currentP, tmp);
}
currentP->prev = tmp;
}
~circle()
{
monkey *tmp = currentP->next;
while (tmp != currentP)
{
monkey *p = tmp->next;
delete tmp;
tmp = p;
}
delete currentP;
}
void move(int n)
{
for (int i = 0;i < n;++i)
currentP = currentP->next;
}
void del()
{
monkey *p = currentP->prev;
p->next = currentP->next;
p->next->prev = p;
delete currentP;
currentP = p->next;
}
int getNum() const
{
return currentP->num;
}
};
int main()
{
int M;
cin >> M;
circle c(M);
int Kn;
int count = M;
for (int n = 1;n <= M - 1;++n)
{
cin >> Kn;
Kn = (Kn-1)%count+1;
c.move(Kn - 1);
c.del();
--count;
}
cout << c.getNum() << endl;
cin.get();
cin.get();
}
1039
#include <iostream>
using namespace std;
struct node
{
int lchild, rchild;
int num;
node() :lchild(-1), rchild(-1) {}
};
node *arr;
void postOrder(int n)
{
if (arr[n].lchild != -1)postOrder(arr[n].lchild);
if (arr[n].rchild != -1)postOrder(arr[n].rchild);
cout << n << ' ';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin >> N;
arr = new node[N+1];
int n, l, r;
for (int i = 1;i <= N;++i)
{
cin >> n >> l >> r;
arr[n].lchild = l;
arr[n].rchild = r;
}
int *q = new int[N];
int front = 0, back = 0;
q[back++] = 1;
arr[1].num = 1;
while (front < back)
{
int tmp = q[front++];
if (arr[tmp].lchild != -1)
{
q[back++] = arr[tmp].lchild;
arr[q[back - 1]].num = 2 * arr[tmp].num;
}
if (arr[tmp].rchild != -1)
{
q[back++] = arr[tmp].rchild;
arr[q[back - 1]].num = 2 * arr[tmp].num + 1;
}
}
for (int i = 1;i <= N;++i)
cout << arr[i].num << ' ';
cout << '\n';
postOrder(1);
cout << '\n';
delete[]q;
delete[] arr;
cin.get();
cin.get();
return 0;
}
1040
#include <iostream>
using namespace std;
struct node
{
int lchild, rchild;
int num;
node() :lchild(-1), rchild(-1) {}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin >> N;
node *arr = new node[N];
int num;
for (int i = 1;i < N;++i)
{
cin >> num;
if (arr[num].lchild == -1)arr[num].lchild = i;
else arr[num].rchild = i;
}
arr[0].num = 1;
int *q = new int[N];
int front = 0, back = 0;
q[back++] = 0;
int tmp;
while (front < back)
{
tmp = q[front++];
cout << tmp << ' ';
if (arr[tmp].lchild != -1)q[back++] = arr[tmp].lchild;
if (arr[tmp].rchild != -1)q[back++] = arr[tmp].rchild;
}
cout << '\n';
delete[]arr;
delete[]q;
cin.get();
cin.get();
return 0;
}
1042
#include <iostream>
using namespace std;
struct node
{
int lchild, rbrother;
int last;
node() :lchild(0), rbrother(0),last(0) {}
};
node *arr;
void preOrder(int root)
{
cout << root << ' ';
int tmp = arr[root].lchild;
while (tmp != 0)
{
preOrder(tmp);
tmp = arr[tmp].rbrother;
}
}
void postOrder(int root)
{
int tmp = arr[root].lchild;
while (tmp != 0)
{
postOrder(tmp);
tmp = arr[tmp].rbrother;
}
cout << root << ' ';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin >> N;
arr = new node[N+1];
int f, l, r;
while(cin>>f>>l>>r)
{
if (arr[f].lchild == 0)
{
arr[f].lchild = l;
arr[l].last = f;
}
if (arr[f].rbrother == 0)
{
arr[f].rbrother = r;
arr[r].last = f;
}
}
int root = 1;
while (arr[root].last != 0) root = arr[root].last;
int *q = new int[N];
int front = 0, back = 0;
preOrder(root);
cout << '\n';
postOrder(root);
cout << '\n';
q[back++] = root;
int tmp;
while (front < back)
{
tmp = q[front++];
cout << tmp << ' ';
tmp = arr[tmp].lchild;
while (tmp != 0)
{
q[back++] = tmp;
tmp = arr[tmp].rbrother;
}
}
cout << '\n';
delete[]arr;
delete[]q;
cin.clear();
cin.get();
return 0;
}
1043
#include <iostream>
using namespace std;
struct node
{
int lchild, rchild;
int num;
node() :lchild(-1), rchild(-1) {}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin >> N;
node *arr = new node[N];
int num;
for (int i = 1;i < N;++i)
{
cin >> num;
if (arr[num].lchild == -1)arr[num].lchild = i;
else arr[num].rchild = i;
}
arr[0].num = 1;
int *q = new int[N];
int front = 0, back = 0;
q[back++] = 0;
int tmp;
while (front < back)
{
tmp = q[front++];
if (arr[tmp].lchild != -1)
{
q[back++] = arr[tmp].lchild;
arr[q[back - 1]].num = arr[tmp].num * 2;
}
if (arr[tmp].rchild != -1)
{
q[back++] = arr[tmp].rchild;
arr[q[back - 1]].num = arr[tmp].num * 2 + 1;
}
}
cout << (arr[tmp].num == N ? "true" : "false") << '\n';
cin.get();
cin.get();
return 0;
}
1048
#include <iostream>
using namespace std;
struct node
{
int lchild, rchild;
int father;
node() :lchild(-1), rchild(-1),father(-1) {}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin >> N;
node *arr = new node[N+1];
int f, l, r;
while(cin>>f>>l>>r)
{
if (arr[f].lchild == -1)
{
arr[f].lchild = l;
arr[l].father = f;
}
if (arr[f].rchild == -1)
{
arr[f].rchild = r;
arr[r].father = f;
}
}
int root = 1;
while (arr[root].father != -1) root = arr[root].father;
int *q = new int[N];
int front = 0, back = 0;
q[back++] = root;
int tmp;
while (front < back)
{
tmp = q[front++];
cout << tmp << '\n';
if (arr[tmp].lchild != -1)q[back++] = arr[tmp].lchild;
if (arr[tmp].rchild != -1)q[back++] = arr[tmp].rchild;
}
delete[]arr;
delete[]q;
cin.get();
cin.get();
return 0;
}
1049
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int stack[1000];
int order[1000];
int T;
cin >> T;
for (int i = 0;i < T;++i)
{
int N, M;
cin >> N >> M;
int top = -1;
for (int j = 0;j < N;++j) cin >> order[j];
int pos = 0;
for (int j = 0;j < N;++j)
{
while (top != -1 && stack[top] == order[pos])
{
--top;
++pos;
}
if (j != order[pos])
{
if (top == M - 1)break;
else stack[++top] = j;
}
else ++pos;
}
while (top != -1 && stack[top] == order[pos])
{
--top;
++pos;
}
cout << (pos == N ? "YES" : "NO") << '\n';
}
cin.get();
cin.get();
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int stack[1000];
int order[1000];
int T;
cin >> T;
for (int i = 0;i < T;++i)
{
int N, M;
cin >> N >> M;
int top = -1;
for (int j = 0;j < N;++j) cin >> order[j];
int pos = 0;
for (int j = 0;j < N;++j)
{
while (top != -1 && stack[top] == order[pos])
{
--top;
++pos;
}
if (j != order[pos])
{
if (top == M - 1)break;
else stack[++top] = j;
}
else ++pos;
}
while (top != -1 && stack[top] == order[pos])
{
--top;
++pos;
}
cout << (pos == N ? "YES" : "NO") << '\n';
}
cin.get();
cin.get();
return 0;
}
1050
#include <iostream>
using namespace std;
class bernoulli
{
private:
struct node
{
int data;
node *son, *brother;
node() {}
node(int x, node *p = NULL, node *q = NULL) :data(x), son(p), brother(q) {}
};
node **forest;
int len;
node* merge(node *t1, node *t2)
{
if (t1->data > t2->data)
return merge(t2, t1);
t2->brother = t1->son;
t1->son = t2;
return t1;
}
void clear(node *p)
{
if (p)
{
node *tmp = p->son;
delete p;
while (tmp)
{
p = tmp->brother;
clear(tmp);
tmp = p;
}
}
}
void doubleSpace()
{
node **tmp = new node*[len * 2]();
for (int i = 0;i < len;++i) tmp[i] = forest[i];
len *= 2;
delete[]forest;
forest = tmp;
}
public:
bernoulli(int initSize=10):len(initSize)
{
forest = new node*[len]();
}
~bernoulli()
{
for (int i = 0;i < len;++i) clear(forest[i]);
delete[]forest;
}
void insert(int x)
{
node *tmp = new node(x);
int i;
for (i = 0;i < len;++i)
{
if (!forest[i]) break;
tmp = merge(forest[i], tmp);
forest[i] = NULL;
}
if (i == len) doubleSpace();
forest[i] = tmp;
}
void merge(bernoulli &other)
{
if (&other == this) return;
while (len <= other.len)doubleSpace();
node* carry = NULL;
for (int i = 0;i < other.len;++i)
{
if (forest[i] && other.forest[i])
{
if (carry)
{
carry = merge(carry, other.forest[i]);
other.forest[i] = NULL;
}
else
{
carry = merge(forest[i], other.forest[i]);
other.forest[i] = forest[i] = NULL;
}
}
else
{
if (carry)
{
if (forest[i] || other.forest[i])
{
carry = merge((other.forest[i] == NULL ? forest[i] : other.forest[i]), carry);
other.forest[i] = forest[i] = NULL;
}
else
{
forest[i] = carry;
carry = NULL;
}
}
else
{
if (!forest[i])
{
forest[i] = other.forest[i];
other.forest[i] = NULL;
}
}
}
}
if (carry) forest[other.len] = carry;
}
int pop()
{
int min = 0x7fffffff;
int pos = -1;
for (int i = 0;i < len;++i)
{
if (forest[i] && forest[i]->data < min)
{
min = forest[i]->data;
pos = i;
}
}
if (pos == -1) return -1;
bernoulli sub(pos+1); //pos可能为0;
int i = pos - 1;
node *res = forest[pos]->son;
while (res)
{
sub.forest[i--] = res;
res = res->brother;
sub.forest[i + 1]->brother = NULL; //因为插入用的是反序。
}
delete forest[pos];
forest[pos] = NULL;
merge(sub);
return min;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M;
cin >> N >> M;
int x, y;
bernoulli *q = new bernoulli[N];
for (int i = 0;i < N;++i)
{
cin >> x;
q[i].insert(x);
}
int command;
for (int i = 0;i < M;++i)
{
cin >> command;
switch (command)
{
case 0:
cin >> x >> y;
q[x].merge(q[y]);
break;
case 1:
cin >> x;
cout << q[x].pop() << '\n';
break;
case 2:
cin >> x >> y;
q[x].insert(y);
}
}
cin.get();
cin.get();
delete[]q;
return 0;
}
#include <iostream>
using namespace std;
class bernoulli
{
private:
struct node
{
int data;
node *son, *brother;
node() {}
node(int x, node *p = NULL, node *q = NULL) :data(x), son(p), brother(q) {}
};
node **forest;
int len;
node* merge(node *t1, node *t2)
{
if (t1->data > t2->data)
return merge(t2, t1);
t2->brother = t1->son;
t1->son = t2;
return t1;
}
void clear(node *p)
{
if (p)
{
node *tmp = p->son;
delete p;
while (tmp)
{
p = tmp->brother;
clear(tmp);
tmp = p;
}
}
}
void doubleSpace()
{
node **tmp = new node*[len * 2]();
for (int i = 0;i < len;++i) tmp[i] = forest[i];
len *= 2;
delete[]forest;
forest = tmp;
}
public:
bernoulli(int initSize=20):len(initSize)
{
forest = new node*[len]();
}
~bernoulli()
{
for (int i = 0;i < len;++i) clear(forest[i]);
delete[]forest;
}
void insert(int x)
{
node *tmp = new node(x);
int i;
for (i = 0;i < len;++i)
{
if (!forest[i]) break;
tmp = merge(forest[i], tmp);
forest[i] = NULL;
}
if (i == len) doubleSpace();
forest[i] = tmp;
}
void merge(bernoulli &other)
{
if (&other == this) return;
while (len <= other.len)doubleSpace();
node* carry = NULL;
for (int i = 0;i < other.len;++i)
{
if (forest[i] && other.forest[i])
{
if (carry)
{
carry = merge(carry, other.forest[i]);
other.forest[i] = NULL;
}
else
{
carry = merge(forest[i], other.forest[i]);
other.forest[i] = forest[i] = NULL;
}
}
else
{
if (carry)
{
if (forest[i] || other.forest[i])
{
carry = merge((other.forest[i] == NULL ? forest[i] : other.forest[i]), carry);
other.forest[i] = forest[i] = NULL;
}
else
{
forest[i] = carry;
carry = NULL;
}
}
else
{
if (!forest[i])
{
forest[i] = other.forest[i];
other.forest[i] = NULL;
}
}
}
}
if (carry) forest[other.len] = carry;
}
int pop()
{
int min = 0x7fffffff;
int pos = -1;
for (int i = 0;i < len;++i)
{
if (forest[i] && forest[i]->data < min)
{
min = forest[i]->data;
pos = i;
}
}
if (pos == -1) return -1;
bernoulli sub(pos+1); //pos可能为0;
int i = pos - 1;
node *res = forest[pos]->son;
while (res)
{
sub.forest[i--] = res;
res = res->brother;
sub.forest[i + 1]->brother = NULL; //因为插入用的是反序。
}
delete forest[pos];
forest[pos] = NULL;
merge(sub);
return min;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M;
cin >> N >> M;
int x, y;
bernoulli *q = new bernoulli[N];
for (int i = 0;i < N;++i)
{
cin >> x;
q[i].insert(x);
}
int command;
for (int i = 0;i < M;++i)
{
cin >> command;
switch (command)
{
case 0:
cin >> x >> y;
q[x].merge(q[y]);
break;
case 1:
cin >> x;
cout << q[x].pop() << '\n';
break;
case 2:
cin >> x >> y;
q[x].insert(y);
}
}
cin.get();
cin.get();
delete[]q;
return 0;
}
#include <iostream>
using namespace std;
class skewHeap
{
private:
struct node
{
int data;
node *lchild, *rchild;
node(int n, node *p = NULL, node *q = NULL) :data(n), lchild(p), rchild(q) {}
};
node *root;
void clear(node *t)
{
if (!t) return;
clear(t->lchild);
clear(t->rchild);
delete t;
}
node *merge(node *t1, node *t2)
{
if (t1 == NULL) return t2;
if (t2 == NULL) return t1;
if (t1->data > t2->data)
{
node *tmp = t1;
t1 = t2;
t2 = tmp;
}
node *p = merge(t1->rchild, t2);
t1->rchild = t1->lchild;
t1->lchild = p;
return t1;
}
public:
skewHeap() :root(NULL) {}
skewHeap(node *p) :root(p) {}
~skewHeap() { clear(root); }
void insert(int x)
{
skewHeap tmp(new node(x));
merge(tmp);
}
int pop()
{
if (!root)return -1;
int min = root->data;
node *tmp = root;
root = merge(root->lchild, root->rchild);
delete tmp;
return min;
}
void merge(skewHeap& another)
{
root = merge(root, another.root);
another.root = NULL;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M;
cin >> N >> M;
int x, y;
skewHeap *q = new skewHeap[N];
for (int i = 0;i < N;++i)
{
cin >> x;
q[i].insert(x);
}
int command;
for (int i = 0;i < M;++i)
{
cin >> command;
switch (command)
{
case 0:
cin >> x >> y;
q[x].merge(q[y]);
break;
case 1:
cin >> x;
cout << q[x].pop() << '\n';
break;
case 2:
cin >> x >> y;
q[x].insert(y);
}
}
cin.get();
cin.get();
delete[]q;
return 0;
}
#include <iostream>
using namespace std;
class bernoulli
{
private:
struct node
{
int data;
node *son, *brother;
node() {}
node(int x, node *p = NULL, node *q = NULL) :data(x), son(p), brother(q) {}
};
node **forest;
int len;
node* merge(node *t1, node *t2)
{
if (t1->data > t2->data)
return merge(t2, t1);
t2->brother = t1->son;
t1->son = t2;
return t1;
}
void clear(node *p)
{
if (p)
{
node *tmp = p->son;
delete p;
while (tmp)
{
p = tmp->brother;
clear(tmp);
tmp = p;
}
}
}
void doubleSpace()
{
node **tmp = new node*[len * 2]();
for (int i = 0;i < len;++i) tmp[i] = forest[i];
len *= 2;
delete[]forest;
forest = tmp;
}
public:
bernoulli(int initSize = 1) :len(initSize)
{
if (len>0)forest = new node*[len]();
else forest = NULL;
}
~bernoulli()
{
for (int i = 0;i < len;++i) clear(forest[i]);
delete[]forest;
}
void insert(int x)
{
node *tmp = new node(x);
int i;
for (i = 0;i < len;++i)
{
if (!forest[i]) break;
tmp = merge(forest[i], tmp);
forest[i] = NULL;
}
if (i == len) doubleSpace();
forest[i] = tmp;
}
void merge(bernoulli &other)
{
if (&other == this) return;
int otherLen;
for (otherLen = other.len - 1;otherLen>=0&&other.forest[otherLen] == NULL;--otherLen);
++otherLen;
while (len <= otherLen)doubleSpace();//如果上面不做如此处理,有可能other中为空。在初始空间很大时,可能堆内存被耗光。从而runtime error,不是内存超出限制。
node* carry = NULL;
for (int i = 0;i < otherLen;++i)
{
if (forest[i] && other.forest[i])
{
if (carry)
{
carry = merge(carry, other.forest[i]);
other.forest[i] = NULL;
}
else
{
carry = merge(forest[i], other.forest[i]);
other.forest[i] = forest[i] = NULL;
}
}
else
{
if (carry)
{
if (forest[i] || other.forest[i])
{
carry = merge((other.forest[i] == NULL ? forest[i] : other.forest[i]), carry);
other.forest[i] = forest[i] = NULL;
}
else
{
forest[i] = carry;
carry = NULL;
}
}
else
{
if (!forest[i])
{
forest[i] = other.forest[i];
other.forest[i] = NULL;
}
}
}
}
for (int i = otherLen;i < len;++i)
{
if (carry)
{
if (forest[i])
{
carry = merge(forest[i], carry);
forest[i] = NULL;
}
else
{
forest[i] = carry;
carry = NULL;
}
}
else break;
}
if (carry)
{
doubleSpace();
forest[len / 2] = carry;
}
}
int pop()
{
int min = 0x7fffffff;
int pos = -1;
for (int i = 0;i < len;++i)
{
if (forest[i] && forest[i]->data < min)
{
min = forest[i]->data;
pos = i;
}
}
if (pos == -1) return -1;
bernoulli sub(pos); //pos可能为0;
int i = pos - 1;
node *res = forest[pos]->son;
while (res)
{
sub.forest[i--] = res;
res = res->brother;
sub.forest[i + 1]->brother = NULL; //因为插入用的是反序。
}
delete forest[pos];
forest[pos] = NULL;
merge(sub);
return min;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M;
cin >> N >> M;
int x, y;
bernoulli *q = new bernoulli[N];
for (int i = 0;i < N;++i)
{
cin >> x;
q[i].insert(x);
}
int command;
for (int i = 0;i < M;++i)
{
cin >> command;
switch (command)
{
case 0:
cin >> x >> y;
q[x].merge(q[y]);
break;
case 1:
cin >> x;
cout << q[x].pop() << '\n';
break;
case 2:
cin >> x >> y;
q[x].insert(y);
}
}
cin.get();
cin.get();
delete[]q;
return 0;
}
#include <iostream>
using namespace std;
class bernoulli
{
private:
struct node
{
int data;
node *son, *brother;
node() {}
node(int x, node *p = NULL, node *q = NULL) :data(x), son(p), brother(q) {}
};
node **forest;
int len;
node* merge(node *t1, node *t2)
{
if (t1->data > t2->data)
return merge(t2, t1);
t2->brother = t1->son;
t1->son = t2;
return t1;
}
void clear(node *p)
{
if (p)
{
node *tmp = p->son;
delete p;
while (tmp)
{
p = tmp->brother;
clear(tmp);
tmp = p;
}
}
}
void doubleSpace()
{
node **tmp = new node*[len * 2]();
for (int i = 0;i < len;++i) tmp[i] = forest[i];
len *= 2;
delete[]forest;
forest = tmp;
}
public:
bernoulli(int initSize = 30) :len(initSize)
{
if (len>0)forest = new node*[len]();
else forest = NULL;
}
~bernoulli()
{
for (int i = 0;i < len;++i) clear(forest[i]);
delete[]forest;
}
void insert(int x)
{
node *tmp = new node(x);
int i;
for (i = 0;i < len;++i)
{
if (!forest[i]) break;
tmp = merge(forest[i], tmp);
forest[i] = NULL;
}
if (i == len) doubleSpace();
forest[i] = tmp;
}
void merge(bernoulli &other)
{
if (&other == this) return;
int otherLen;
for (otherLen = other.len - 1;otherLen>=0&&other.forest[otherLen] == NULL;--otherLen);
++otherLen;
while (len <= otherLen)doubleSpace();
node* carry = NULL;
for (int i = 0;i < otherLen;++i)
{
if (forest[i] && other.forest[i])
{
if (carry)
{
carry = merge(carry, other.forest[i]);
other.forest[i] = NULL;
}
else
{
carry = merge(forest[i], other.forest[i]);
other.forest[i] = forest[i] = NULL;
}
}
else
{
if (carry)
{
if (forest[i] || other.forest[i])
{
carry = merge((other.forest[i] == NULL ? forest[i] : other.forest[i]), carry);
other.forest[i] = forest[i] = NULL;
}
else
{
forest[i] = carry;
carry = NULL;
}
}
else
{
if (!forest[i])
{
forest[i] = other.forest[i];
other.forest[i] = NULL;
}
}
}
}
for (int i = otherLen;i < len;++i)
{
if (carry)
{
if (forest[i])
{
carry = merge(forest[i], carry);
forest[i] = NULL;
}
else
{
forest[i] = carry;
carry = NULL;
}
}
else break;
}
if (carry)
{
doubleSpace();
forest[len / 2] = carry;
}
}
int pop()
{
int min = 0x7fffffff;
int pos = -1;
for (int i = 0;i < len;++i)
{
if (forest[i] && forest[i]->data < min)
{
min = forest[i]->data;
pos = i;
}
}
if (pos == -1) return -1;
bernoulli sub(pos); //pos可能为0;
int i = pos - 1;
node *res = forest[pos]->son;
while (res)
{
sub.forest[i--] = res;
res = res->brother;
sub.forest[i + 1]->brother = NULL; //因为插入用的是反序。
}
delete forest[pos];
forest[pos] = NULL;
merge(sub);
return min;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M;
cin >> N >> M;
int x, y;
bernoulli *q = new bernoulli[N];
for (int i = 0;i < N;++i)
{
cin >> x;
q[i].insert(x);
}
int command;
for (int i = 0;i < M;++i)
{
cin >> command;
switch (command)
{
case 0:
cin >> x >> y;
q[x].merge(q[y]);
break;
case 1:
cin >> x;
cout << q[x].pop() << '\n';
break;
case 2:
cin >> x >> y;
q[x].insert(y);
}
}
cin.get();
cin.get();
delete[]q;
return 0;
}