目录
7.Deduplication on a Linked List
1.链表的定义
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<ctime>
#include<queue>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
struct node{
int data;
node* next;
};
node* create(int array[]){
node *head,*pre,*p;
head=new node;
head->next=NULL;
pre=head;
for(int i=0;i<5;i++){
p=new node;
p->data=array[i];
p->next=NULL;
pre->next=p;
pre=p;
}
return head;
}
int main(){
int array[5]={0,1,2,3,4};
node *l=create(array);
l=l->next;
while(l!=NULL){
printf("%d",l->data);
l=l->next;
}
return 0;
}
int search(node* head,int x){
int count=0;
node* p=head->next;
while(p!=NULL){
if(p->data==x) count++;
p=p->next;
}
return count;
}
void insert(node *head,int pos,int x){
node* p=head;
for(int i=0;i<pos-1;i++){
p=p->next;
}
node* q=new node;
q->next=p->next;
q->data=x;
p->next=q;
}
void delete(node* head,int x){
node* p=head->next;
node* pre=head;
while(p!=NULL){
if(p->data==x){
pre->next=p->next;
delete(p);
p=pre.next;
}else{
pre=p;
p=p->next;
}
}
}
2.Sharing
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<ctime>
#include<queue>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=100005;
struct node{
char letter;
int next;
bool flag;
}martix[maxn];
int main(){
int s1,s2,n;
scanf("%d %d %d",&s1,&s2,&n);
int a1,a2;
char l;
for(int i=0;i<n;i++){
scanf("%d %c %d",&a1,&l,&a2);
martix[a1].letter=l;
martix[a1].next=a2;
}
for(int i=0;i<maxn;i++){
martix[i].flag=false;
}
for(int p=s1;p!=-1;p=martix[p].next){
martix[p].flag=true;
}
int q;
for(q=s2;q!=-1;q=martix[q].next){
if(martix[q].flag==true) break;
}
if(q==-1) printf("-1\n");
else printf("%05d\n",q);
return 0;
}
3.Linked List Sorting
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<ctime>
#include<queue>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=100005;
struct node{
int address;
int key;
int next;
bool flag;
}martix[maxn];
void init(){
for(int i=0;i<maxn;i++){
martix[i].flag=false;
}
}
bool cmp(node a,node b){
if(a.flag==false||b.flag==false){
return a.flag>b.flag;
}else{
return a.key<b.key;
}
}
int main(){
init();
int n,head;
scanf("%d %d",&n,&head);
int address,key,next;
for(int i=0;i<n;i++){
scanf("%d",&address);
martix[address].address=address;
scanf("%d %d",&key,&next);
martix[address].next=next;
martix[address].key=key;
}
int p=head,count=0;
while(p!=-1){
martix[p].flag=true;
count++;
p=martix[p].next;
}
if(count==0){
printf("0 -1");
return 0;
}
sort(martix,martix+maxn,cmp);
printf("%d %05d\n",count,martix[0].address);
for(int i=0;i<count;i++){
if(i!=count-1) printf("%05d %d %05d\n",martix[i].address,martix[i].key,martix[i+1].address);
else printf("%05d %d -1\n",martix[i].address,martix[i].key);
}
return 0;
}
4.Pop Sequence
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<ctime>
#include<queue>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=1010;
int martix[maxn];
stack<int> s;
int main(){
int m,n,k;
scanf("%d%d%d",&m,&n,&k);
while(k--){
while(!s.empty()) s.pop();
for(int i=0;i<n;i++){
scanf("%d",&martix[i]);
}
int current=0;
bool flag=true;
for(int i=1;i<=n;i++){
s.push(i);
if(s.size()>m){
flag=false;
break;
}
while(!s.empty()&&martix[current]==s.top()){
current++;
s.pop();
}
}
if(flag&&s.empty()) printf("YES\n");
else printf("NO\n");
}
return 0;
}
5.Mice and Rice
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<ctime>
#include<queue>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=1010;
struct mice{
int weight;
int rank;
}martix[maxn];
int main(){
int np,ng;
scanf("%d %d",&np,&ng);
for(int i=0;i<np;i++){
scanf("%d",&martix[i]);
}
queue<int> q;
int order;
for(int i=0;i<np;i++){
scanf("%d",&order);
q.push(order);
}
int mice_num=np,group;
while(q.size()!=1){
if(mice_num%ng==0) group=mice_num/ng;
else group=mice_num/ng+1;
for(int i=0;i<group;i++){
int max_=q.front();
for(int j=0;j<ng;j++){
if(i*ng+j>=mice_num) break;
int front=q.front();
if(martix[front].weight>martix[max_].weight){
max_=front;
}
martix[front].rank=group+1;
q.pop();
}
q.push(max_);
}
mice_num=group;
}
martix[q.front()].rank=1;
for(int i=0;i<np;i++){
printf("%d",martix[i].rank);
if(i<np-1) printf(" ");
}
return 0;
}
6.Reversing Linked List
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<ctime>
#include<queue>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=100005;
struct node{
int address,value,next;
int order;
}martix[maxn];
bool cmp(node a,node b){
return a.order<b.order;
}
int main(){
for(int i=0;i<maxn;i++){
martix[i].order=maxn;
}
int begin,n,step;
scanf("%d%d%d",&begin,&n,&step);
int address;
for(int i=0;i<n;i++){
scanf("%d",&address);
scanf("%d %d",&martix[address].value,&martix[address].next);
martix[address].address=address;
}
int p=begin,count=0;
while(p!=-1){
martix[p].order=count++;
p=martix[p].next;
}
sort(martix,martix+maxn,cmp);
n=count;
for(int i=0;i<n/step;i++){
for(int j=(i+1)*step-1;j>i*step;j--){
printf("%05d %d %05d\n",martix[j].address,martix[j].value,martix[j-1].address);
}
printf("%05d %d ",martix[i*step].address,martix[i*step].value);
if(i<n/step-1) printf("%05d\n",martix[(i+2)*step-1].address);
else{
if(n%step==0) printf("-1\n");
else{
printf("%05d\n",martix[(i+1)*step].address);
for(int i=n/step*step;i<n;i++){
printf("%05d %d ",martix[i].address,martix[i].value);
if(i<n-1) printf("%05d\n",martix[i+1].address);
else printf("-1\n");
}
}
}
}
return 0;
}
7.Deduplication on a Linked List
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<ctime>
#include<queue>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=100005;
struct node{
int address,value,next;
int order;
}martix[maxn];
bool cmp(node a,node b){
return a.order<b.order;
}
bool exist[maxn]={0};
int main(){
for(int i=0;i<maxn;i++) martix[i].order=2*maxn;
int begin,n;
scanf("%d%d",&begin,&n);
int address;
for(int i=0;i<n;i++){
scanf("%d",&address);
scanf("%d%d",&martix[address].value,&martix[address].next);
martix[address].address=address;
}
int p=begin,valid=0,unvalid=0;
while(p!=-1){
if(!exist[abs(martix[p].value)]){
martix[p].order=valid++;
exist[abs(martix[p].value)]=1;
}else{
martix[p].order=maxn+unvalid;
unvalid++;
}
p=martix[p].next;
}
sort(martix,martix+maxn,cmp);
for(int i=0;i<valid;i++){
if(i!=valid-1){
printf("%05d %d %05d\n",martix[i].address,martix[i].value,martix[i+1].address);
}else{
printf("%05d %d -1\n",martix[i].address,martix[i].value);
}
}
for(int i=valid;i<valid+unvalid;i++){
if(i!=valid+unvalid-1){
printf("%05d %d %05d\n",martix[i].address,martix[i].value,martix[i+1].address);
}else{
printf("%05d %d -1\n",martix[i].address,martix[i].value);
}
}
return 0;
}