hdu 贪心

    技术2024-12-01  12

    树 二分 搜索 +1 回溯法 递归和回溯 树形dp 没时间了,就这样吧

    贪心 hdu 1009 #include<stdio.h> #include<string> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn = 1005; const int inf = 1e5; int m,n; struct node{ int j; int f; double v; }c[maxn]; bool cmp1(node a,node b){ return a.v < b.v; } int main(){ while(scanf("%d %d",&m,&n)){ if((n == -1)&&(m == -1)){ break; } double ans = 0; for(int i = 0;i < n;i ++){ scanf("%d %d",&c[i].j,&c[i].f); } for(int i = 0;i < n;i ++){ if(c[i].j != 0){ c[i].v = (c[i].f *1.0)/ (1.0*c[i].j); // printf("1:%d %d %lf\n",c[i].f,c[i].j,c[i].v); } else c[i].v = inf; } sort(c,c+n,cmp1); // for(int i = 0;i < n;i ++){ // printf("%d %d %lf\n",c[i].f,c[i].j,c[i].v); // } for(int i = 0;i < n;i ++){ if(m > c[i].f){ m -= c[i].f; ans += c[i].j; } else{ double temp = (m*1.0 )/ c[i].f; ans += c[i].j*temp; m = 0; } if(m == 0) break; } printf("%.3lf\n",ans); } return 0; } hdu 1789 #include<stdio.h> #include<memory.h> #include<algorithm> using namespace std; const int maxn = 1005; int n; int t; int vis[maxn]; struct node{ int date; int down; }a[maxn]; bool cmp1(node a,node b){ return a.down > b.down; } int main(){ scanf("%d",&t); while(t -- ){ scanf("%d",&n);//表示作业的数量 memset(vis,0,sizeof(vis)); for(int i = 0;i < n;i ++){ scanf("%d",&a[i].date); } for(int i = 0;i < n;i ++){ scanf("%d",&a[i].down); } sort(a,a+n,cmp1); int ans = 0; for(int i = 0 ;i < n;i ++){ int flag = 0; for(int j = a[i].date;j > 0;j --){ if(vis[j] == 0){//这一天没有被访问过 vis[j] = 1; flag = 1; break; } } if(flag == 0){ ans += a[i].down; } } printf("%d\n",ans); } return 0; } //16:13 哈夫曼编码权值的应用 #include<stdio.h> #include<memory.h> #include<algorithm> #include<string> #include<string.h> #include<cstring> using namespace std; const int maxn = 1005; const int inf = 1e9; int t; int m; char s[maxn]; int word[26]; int main(){ scanf("%d",&t); while(t -- ){ scanf("%d",&m); scanf("%s",&s); int sum = 0; memset(word,0,sizeof(word)); int len = strlen(s); for(int i = 0;i < len;i ++){ word[s[i] - 'a'] ++; } for(int i = 0;i < 26 ;i ++){ if(word[i] == 0) word[i] = inf; } sort(word,word+26);//将字符串按照从小到大排序 if(word[1] == inf){//在测试的时候,只有一个字母 sum = word[0] ; } while(word[1] != inf){ sum += word[0] + word[1]; word[0] += word[1] ; word[1] = inf; sort(word,word+26); } if(sum <= m) printf("yes\n"); else printf("no\n"); } return 0; }

    树:

    Processed: 0.072, SQL: 9