NO IMAGE

L2-009. 搶紅包

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int N;
struct node{
int id;
double money;
int cnt;
node(){
id=-1;
money=0;
cnt=0;
}
}person[10001];
int com(struct node a,struct node b){
if(a.money==b.money){
if(a.cnt==b.cnt){
return a.id<b.id;
}
return a.cnt>b.cnt;
}
return a.money>b.money;
}
int main(){
int id,k;
double mon;
scanf("%d",&N);
for(int i=1;i<=N;i  ){
scanf("%d",&k);
person[i].id=i;
double total=0;
for(int j=0;j<k;j  ){
scanf("%d%lf",&id,&mon);
total =mon;//若在這裡轉換成分,第一個case錯誤 
person[id].money =mon;
person[id].cnt  ;
}
person[i].money-=total;
}
sort(person 1,person N 1,com);
for(int i=1;i<=N;i  ){
printf("%d %.2lf\n",person[i].id,(person[i].money*1.0)/100);
}
return 0;
}

L2-010 搶座位 (並查集)

#include<cstring>
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstdlib>
using namespace std;
int relation[101][101],N,M,K;
int f[101];
int find(int x){
if(x!=f[x]){
f[x]=find(f[x]);
}
return f[x];
}
int main(){
int a,b,c;
scanf("%d%d%d",&N,&M,&K);
for(int i=0;i<=N;i  ){
f[i]=i;
}
for(int i=0;i<M;i  ){
scanf("%d%d%d",&a,&b,&c);
if(c==-1){
relation[a][b]=c;
relation[b][a]=c;
continue;
}
int x=find(a);
int y=find(b);
if(x!=y){
f[x]=y;//root不同時並在一起; 
}
}
for(int i=0;i<K;i  ){
scanf("%d%d",&a,&b);
find(a);find(b);
if(f[a]==f[b]&&(relation[a][b]!=-1)){
printf("No problem\n");
}	
else if(f[a]==f[b]&&(relation[a][b]==-1)){
printf("OK but...\n");
}
else if(f[a]!=f[b]&&(relation[a][b]!=-1)){
printf("OK\n");
}
else{
printf("No way\n");
}
}
return 0;
}

L2-011 玩轉二叉樹

#include<cstring>
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstdlib>
using namespace std;
int in[31],pre[31];
struct node{
int num;
struct node*left;
struct node*right;
};
typedef struct node*Tree;
Tree build(int in_s,int in_e,int pre_s,int pre_e){
if((in_e-in_s)!=(pre_e-pre_s)) return NULL;
if(in_s>in_e||(pre_s>pre_e)) return NULL;
Tree t=(Tree)malloc(sizeof(struct node));
if(!t) return 0;
t->num=pre[pre_s];
t->left=NULL;
t->right=NULL;
int i;
for(i=in_s;i<=in_e;i  ){
if(in[i]==pre[pre_s]){
break;
}
}
if(i>in_s){
int ll=i-in_s;
if(ll==0) return t;
t->left=build(in_s,i-1,pre_s 1,pre_s ll);
}
if(i<in_e){
int ll=in_e-i;
if(ll==0) return t;
t->right=build(i 1,in_e,pre_e-ll 1,pre_e);
}
return t;
}
void level(Tree T){
if(T==NULL) return ;
queue<Tree>q;
q.push(T);
Tree t;
int flag=0;
while(!q.empty()){
t=q.front();
q.pop();
if(!flag){
printf("%d",T->num);
flag=1;
}
else{
printf(" %d",t->num);
}
if(t->right){
q.push(t->right);
}
if(t->left){
q.push(t->left);
}
}
return ;
}
int main(){
int N;
Tree T;
scanf("%d",&N);
for(int i=0;i<N;i  ){
scanf("%d",&in[i]);
}
for(int i=0;i<N;i  ){
scanf("%d",&pre[i]);
}
T=build(0,N-1,0,N-1);
level(T);
return 0;
}

L2-012 關於堆的判斷

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int N,M;
int h[1001];
void update(int n){
if(n==1) return ;
int t;
int child=n,parent;
for(;child>1;child=parent){
parent=child/2;
if(h[child]<h[parent]){
t=h[child];
h[child]=h[parent];
h[parent]=t;  
}
}
return ;
}
int find(int a,int b,int num){
//  cout<<a<<' '<<b<<endl;
int child,x,y;
for(int i=1;i<=N;i  ){
if(h[i]==a){
x=i;
}
if(h[i]==b){
y=i;
}
}
//  cout<<x<<' '<<y<<' '<<num<<endl;
if(num==1){
if(x/2==y/2&&(x>1&&y!=x)){
return 1;
}
else{
return 0;
}
}
else if(num==2){
if(y/2==x&&(x>0)) return 1;
else return 0;
}
else{
if(x/2==y&&(y>0)) return 1;
else return 0;
}
return 0;
}
int main(){
int tmp;
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i  ){
scanf("%d",&h[i]);
update(i);
}
//  for(int i=1;i<=N;i  ){
//  	cout<<h[i]<<' ';
//  }
getchar();
string str[7];
int j;
for(int i=1;i<=M;i  ){
for(j=0;;j  ){
cin>>str[j];
char ch=getchar();
if(ch=='\n') break;
}
if(j==3){
int tmp;
sscanf(str[0].c_str(),"%d",&tmp);
if(h[1]==tmp){
printf("T\n");
}
else{
printf("F\n");
}
}
else if(j==4){
int tmp1,tmp2;
sscanf(str[0].c_str(),"%d",&tmp1);
sscanf(str[2].c_str(),"%d",&tmp2);
if(find(tmp1,tmp2,1)){
printf("T\n");
}
else{
printf("F\n");
}
}
else if(j==5){
string ss=str[3];
int tmp1,tmp2;
sscanf(str[0].c_str(),"%d",&tmp1);
sscanf(str[5].c_str(),"%d",&tmp2);
if(ss=="parent"){
if(find(tmp1,tmp2,2)){
printf("T\n");
}
else{
printf("F\n");
}
}
else if(ss=="child"){
if(find(tmp1,tmp2,3)){
printf("T\n");
}
else{
printf("F\n");
}
}
}
}
return 0;
}