第二次集训——2011年2月27日

    技术2022-05-20  43

          很侥幸,昨天迟回去,队长刚好没在,躲过了一劫,但是要牢记,不能超假啊,后果很严重的。

          今天感到了集训的自由性~~上午还是做昨天的题目,有点反感,就去做09年的校赛题目,发现一道很好的题目,杨辉三角

          题目大意是给出在杨辉三角中的相邻两个数的位置,求出在这个图形中的位置。

          题目数据是给出的数在LONG INT范围内,可是具体行列到哪种程度不敢想象~~~

          当时做的人基本都是混过去的,就是说默认在INT64范围内做,无法顾及到很多数据,所以题目的数据弱,竟然都过了~~~

          我想到了解方程的方法,根据两个值列两个方程,但是很显然这是个有多个解的方程,解还得去验证到底对不对,中间试解的过程比较慢,但由于题目数据的弱,行数不会超过50行,所以试根也很快了,但是遇到犀利一点的数据怎么办呢?比如99999999 1

          恰巧刚看过扩展的欧几里得算GCD的方法,有了大概印象,可以求一个一元二次方程整数解,然后就可以把通解表示出来,这道题需要这个,这样一次就可以得一个根,速度接近线性了!争取在两天内把它编写出来!

     

     

          下午是个郁闷的下午。题目都是很恶心的,描述一大堆,做做出了2题。

    A题:好像是跟NOIP差不多的题目

    算法:二分+贪心

             由于要使得最小的使用度尽量大,二分使用度;每样物品必须要出现购买一样,这样就可以购买使用度符合范围的,且MONEY尽量少的,就可以判断当前解是否可行;

    注意了,二分的端点很重要的,这里左端点可以选择0,右端点选择出现过的最大使用度。呃~~~Wrong了,应该要加1,因为左边是可以取到的,右边不可取到,这样要考虑到最好的情况:最后的答案刚好是最大的使用度。

    Description Recently your team noticed that the computer you use to practice for programming contests is not good enough anymore. Therefore, you decide to buy a new computer. To make the ideal computer for your needs, you decide to buy separate components and assemble the computer yourself. You need to buy exactly one of each type of component. The problem is which components to buy. As you all know, the quality of a computer is equal to the quality of its weakest component. Therefore, you want to maximize the quality of the component with the lowest quality, while not exceeding your budget. Input On the first line one positive number: the number of testcases, at most 100. After that per testcase: • One line with two integers: 1 ≤ n ≤ 1 000, the number of available components and 1 ≤ b ≤ 1 000 000 000, your budget. • n lines in the following format: “type name price quality”, where type is a string with the type of the component, name is a string with the unique name of the com- ponent, price is an integer (0 ≤ price ≤ 1 000 000) which represents the price of the component and quality is an integer (0 ≤ quality ≤ 1 000 000 000) which represents the quality of the component (higher is better). The strings contain only letters, digits and underscores and have a maximal length of 20 characters. It will always possible to construct a computer with your budget. Output Per testcase: • One line with one integer: the maximal possible quality. Sample Input 118 800processor 3500_MHz 66 5processor 4200_MHz 103 7processor 5000_MHz 156 9processor 6000_MHz 219 12memory 1_GB 35 3memory 2_GB 88 6memory 4_GB 170 12mainbord all_onboard 52 10harddisk 250_GB 54 10harddisk 500_FB 99 12casing midi 36 10monitor 17_inch 157 5monitor 19_inch 175 7monitor 20_inch 210 9monitor 22_inch 293 12mouse cordless_optical 18 12mouse microsoft 30 9keyboard office 4 10 Sample Output 9

     

    代码:

    #include <stdio.h> #include <string.h> const int maxn = 1010; int price[maxn], quality[maxn]; char name[maxn][22], kind[22], type[22]; int head[maxn], next[maxn], tot; int check() { int i, j; for (i = 1; i <= tot; i++) { bool flag = true; if (strlen(name[i]) != strlen(kind)) flag = false; else for (j = 0; j < strlen(kind); j++) if (name[i][j] != kind[j]) { flag = false; break; } if (flag) return i; } tot++; memcpy(name[tot],kind,sizeof(kind)); return tot; } int main() { int cases; scanf("%d", &cases); for (;cases>0; cases--) { int n, money, i, j; scanf("%d %d/n", &n, &money); tot = 0;//from 1.. memset(head,-1,sizeof(head)); int left = 0, right = 0; for (i = 0; i < n; i++) { scanf("%s %s %d %d/n", kind, type, &price[i], &quality[i]); if (quality[i] > right) right = quality[i]; int now = check(); next[i] = head[now]; head[now] = i; } //for (i = 1; i <= tot; i++) printf("%s/n", name[i]); //printf("%d/n",tot); right++;//notice that right can't be reached so right should be bigger than the maximal quality while (left+1 < right) { int mid = (left+right)/2, nowcost = 0; //printf("mid%d/n", mid); for (i = 1; i <= tot; i++) { int mincost = 1 << 30; for (j = head[i]; j != -1; j = next[j]) if (quality[j] >= mid && price[j] < mincost) mincost = price[j]; nowcost += mincost; if (mincost >= 1 << 30) break; } if (nowcost <= money) left = mid; else right = mid; } printf("%d/n", left); } return 0; }

     


    最新回复(0)