LA3971组装电脑
生活随笔
收集整理的這篇文章主要介紹了
LA3971组装电脑
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意:
? ? ? 你有b塊錢,想要組裝一臺電腦,給你提供一些零件,每種零件提供一個或幾個,組裝電腦的前提是每種零件只能也必須選擇一個,每種零件都有自己的種類,名字,價格,還有品質,要求是在能配成電腦的前提下所有零件中最小的品質最大(品質越大越好)。
思路:
? ? ? 最小的最大,第一反應就是二分,這個題目也不例外,我們只要二分品質就行了,品質的數據感覺比較大,但是直接去枚舉應該也能過,如果擔心過不了可以先把零件中所有涉及的品質都拿出來,答案肯定是這些數據中的一個,我們只要sort下,然后去二分枚舉sort后的品質數組,每次枚舉我們都會得到一個當前的品質值,對于每種物品,我們肯定是選擇品質值滿足要求的最小花費的那個零件,其他的沒什么,細心點就行了,具體細節可以看代碼。
? ? ?
? ? ?
? ? ??
#include<map>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#define N 1000 + 10
using namespace std;
typedef struct
{
? ?int jg ,pz;
? ?char str[22];
}NODE;
NODE node[N];
map<string ,int>mark;
int tmp[N] ,P[N] ,nowidp;
bool camp(NODE a, NODE b)
{
? ? return a.jg < b.jg;
}
bool ok(int nowpz ,int n ,int szl ,int b)
{
? ? mark.clear();
? ? int sszl = 0 ,nowb = 0;
? ? for(int i = 1 ;i <= n ;i ++)
? ? {
? ? ? ? if(node[i].pz < nowpz) continue;
? ? ? ? if(!mark[node[i].str]) sszl ++ ,nowb += node[i].jg;
? ? ? ? mark[node[i].str] = 1;
? ? }
? ? return sszl == szl && nowb <= b;
}
? ? ? ??
? ? ? ?
? ?
int main ()
{
? ? int n ,b ,i ,szl ,t;
? ? char str[22];
? ? scanf("%d" ,&t);
? ? while(t--)
? ? {
? ? ? ? scanf("%d %d" ,&n ,&b);
? ? ? ? mark.clear();
? ? ? ? szl = 0;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? {
? ? ? ? ? ?scanf("%s %s %d %d" ,node[i].str ,str ,&node[i].jg ,&node[i].pz);
? ? ? ? ? ?if(!mark[node[i].str]) szl ++;
? ? ? ? ? ?mark[node[i].str] = 1;
? ? ? ? ? ?tmp[i] = node[i].pz;
? ? ? ? }
? ? ? ? sort(node + 1 ,node + n + 1 ,camp);
? ? ? ? sort(tmp + 1 ,tmp + n + 1);
? ? ? ? nowidp = 0;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? if(i == 1 || tmp[i] != tmp[i-1])
? ? ? ? P[++nowidp] = tmp[i];
? ? ? ??
? ? ? ? int low = 1 ,up = nowidp ,mid ,Ans = P[1];
? ? ? ? while(low <= up)
? ? ? ? {
? ? ? ? ? ?mid = (low + up) / 2;
? ? ? ? ? ?if(ok(P[mid] ,n ,szl ,b))
? ? ? ? ? ?{
? ? ? ? ? ? ? Ans = P[mid];
? ? ? ? ? ? ? low = mid + 1;
? ? ? ? ? ?}
? ? ? ? ? ?else up = mid - 1;
? ? ? ? }
? ? ? ? printf("%d\n" ,Ans);
? ? }
? ? return 0;
}
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ?? 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
? ? ? 你有b塊錢,想要組裝一臺電腦,給你提供一些零件,每種零件提供一個或幾個,組裝電腦的前提是每種零件只能也必須選擇一個,每種零件都有自己的種類,名字,價格,還有品質,要求是在能配成電腦的前提下所有零件中最小的品質最大(品質越大越好)。
思路:
? ? ? 最小的最大,第一反應就是二分,這個題目也不例外,我們只要二分品質就行了,品質的數據感覺比較大,但是直接去枚舉應該也能過,如果擔心過不了可以先把零件中所有涉及的品質都拿出來,答案肯定是這些數據中的一個,我們只要sort下,然后去二分枚舉sort后的品質數組,每次枚舉我們都會得到一個當前的品質值,對于每種物品,我們肯定是選擇品質值滿足要求的最小花費的那個零件,其他的沒什么,細心點就行了,具體細節可以看代碼。
? ? ?
? ? ?
? ? ??
#include<map>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#define N 1000 + 10
using namespace std;
typedef struct
{
? ?int jg ,pz;
? ?char str[22];
}NODE;
NODE node[N];
map<string ,int>mark;
int tmp[N] ,P[N] ,nowidp;
bool camp(NODE a, NODE b)
{
? ? return a.jg < b.jg;
}
bool ok(int nowpz ,int n ,int szl ,int b)
{
? ? mark.clear();
? ? int sszl = 0 ,nowb = 0;
? ? for(int i = 1 ;i <= n ;i ++)
? ? {
? ? ? ? if(node[i].pz < nowpz) continue;
? ? ? ? if(!mark[node[i].str]) sszl ++ ,nowb += node[i].jg;
? ? ? ? mark[node[i].str] = 1;
? ? }
? ? return sszl == szl && nowb <= b;
}
? ? ? ??
? ? ? ?
? ?
int main ()
{
? ? int n ,b ,i ,szl ,t;
? ? char str[22];
? ? scanf("%d" ,&t);
? ? while(t--)
? ? {
? ? ? ? scanf("%d %d" ,&n ,&b);
? ? ? ? mark.clear();
? ? ? ? szl = 0;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? {
? ? ? ? ? ?scanf("%s %s %d %d" ,node[i].str ,str ,&node[i].jg ,&node[i].pz);
? ? ? ? ? ?if(!mark[node[i].str]) szl ++;
? ? ? ? ? ?mark[node[i].str] = 1;
? ? ? ? ? ?tmp[i] = node[i].pz;
? ? ? ? }
? ? ? ? sort(node + 1 ,node + n + 1 ,camp);
? ? ? ? sort(tmp + 1 ,tmp + n + 1);
? ? ? ? nowidp = 0;
? ? ? ? for(i = 1 ;i <= n ;i ++)
? ? ? ? if(i == 1 || tmp[i] != tmp[i-1])
? ? ? ? P[++nowidp] = tmp[i];
? ? ? ??
? ? ? ? int low = 1 ,up = nowidp ,mid ,Ans = P[1];
? ? ? ? while(low <= up)
? ? ? ? {
? ? ? ? ? ?mid = (low + up) / 2;
? ? ? ? ? ?if(ok(P[mid] ,n ,szl ,b))
? ? ? ? ? ?{
? ? ? ? ? ? ? Ans = P[mid];
? ? ? ? ? ? ? low = mid + 1;
? ? ? ? ? ?}
? ? ? ? ? ?else up = mid - 1;
? ? ? ? }
? ? ? ? printf("%d\n" ,Ans);
? ? }
? ? return 0;
}
? ? ? ??
? ? ? ??
? ? ? ??
? ? ? ??
? ?? 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的LA3971组装电脑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LA3905流星
- 下一篇: LA3989女士的选择