↓ Skip to main content
  1. Posts/

codeforces #413 B T-shirt buying (贪心)

·699 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:有n个T恤,每个价格都不同,有三种颜色,分别用1,2,3表示,每件T恤给出前xiong和后背的颜色。现在有m个顾客排成一队,对于每个顾客,给出他喜欢的颜色,只要一个T恤的前xiong或者后背的颜色之一满足该颜色即可。顾客总希望买符合他喜欢颜色的T恤中价格最低的。现在问每个顾客买到的T恤的价格,如果某个顾客没有买T恤,输出-1

思路:贪一下?

对于每个颜色,找到价格最低的。记录的时候顺便记录id,以标记某件有2个颜色的衣服是否卖出去了。

1A美滋滋

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2017年05月11日 星期四 15时29分58秒
  4File Name :B.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define PB push_back
 20#define fst first
 21#define sec second
 22#define lson l,m,rt<<1
 23#define rson m+1,r,rt<<1|1
 24#define ms(a,x) memset(a,x,sizeof(a))
 25typedef long long LL;
 26#define pi pair < int ,int >
 27#define MP make_pair
 28
 29using namespace std;
 30const double eps = 1E-8;
 31const int dx4[4]={1,0,0,-1};
 32const int dy4[4]={0,-1,1,0};
 33const int inf = 0x3f3f3f3f;
 34const int N=2E5+7;
 35int n,m;
 36bool vis[N]; //表示某个id的是否被卖出。
 37vector< pair<int,int>>v[4]; //pair<price,id>,id是为了标记是否卖出去了,卖出去就继续找下一个。
 38				    //虽然价格本身唯一,但是太大不适合做标记?
 39struct node
 40{
 41    int p,a,b;
 42    int id;
 43}A[N];
 44
 45bool cmp(node x,node y)
 46{
 47    return x.p<y.p;
 48}
 49vector<int>ans;
 50int main()
 51{
 52	#ifndef  ONLINE_JUDGE
 53	freopen("code/in.txt","r",stdin);
 54  #endif
 55	ms(vis,false);
 56	scanf("%d",&n);
 57	for ( int i = 1 ; i <= n ; i++) scanf("%d",&A[i].p);
 58	for ( int i = 1 ; i <= n ; i++) scanf("%d",&A[i].a);
 59	for ( int i = 1 ; i <= n ; i++) scanf("%d",&A[i].b);
 60	for ( int i = 1 ; i <= n ; i++) A[i].id = i;
 61	//sort(A+1,A+n+1,cmp); //价格升序 没有必要吧。。。
 62
 63	for ( int i = 1 ; i <= n ; i++)
 64	{
 65	    int a = A[i].a;
 66	    int b = A[i].b;
 67	    int p = A[i].p;
 68	    int id = A[i].id;
 69	    if (a==b)
 70	    {
 71		v[a].push_back(make_pair(p,id));
 72	    }else
 73	    {
 74		v[a].push_back(make_pair(p,id));
 75		v[b].push_back(make_pair(p,id));
 76	    }
 77	}
 78
 79
 80
 81	sort(v[1].begin(),v[1].end());
 82	sort(v[2].begin(),v[2].end());
 83	sort(v[3].begin(),v[3].end());
 84	scanf("%d",&m);
 85	//cout<<"m:"<<m<<endl;
 86	int p[4]={0};
 87	int mm = m;
 88	while (m--)
 89	{
 90	    int x;
 91	    scanf("%d",&x);
 92	    while (p[x]<v[x].size()&&vis[v[x][p[x]].second]) p[x]++;
 93	    if (p[x]==v[x].size())
 94	    {
 95		ans.push_back(-1);
 96	    }
 97	    else
 98	    if (!vis[v[x][p[x]].second])
 99	    {
100		vis[v[x][p[x]].second] = true;
101		ans.push_back(v[x][p[x]].first);
102	    }
103	}
104	m = mm;
105	for ( int i = 0 ; i < m ; i++)
106	    printf("%d%c",ans[i],i==m-1?'\n':' ');
107
108
109
110  #ifndef ONLINE_JUDGE
111  fclose(stdin);
112  #endif
113    return 0;
114}

Related

codeforces #375 D. Lakes in Berland (dfs)

·681 words·2 mins
题目链接 题意:nm个格子,有和.两种类型。定义一个湖为边相邻的只有.组成的最大点集合,且任何一个.不在边界上。现在给出一个nm的图保证至少有k个湖。问填多少个.成,才能使得恰好有k个湖。

codeforces #375 C. Polycarp at the Radio (贪心)

·449 words·1 min
题目链接 题意:给出n,m,n个数,对其中的一些数进行修改,要求1..m中出现次数最少的数最大,输出这个最少的数最大是多少,以及修改的次数。 思路:最小的数最多出现n/m次。

whust 2016 #1 D Zhenya moves from the dormitory (贪心,模拟)

·491 words·1 min
题目链接 傻逼模拟。。读完题就ac了。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月07日 星期日 18时04分18秒 4File Name :code/whust2016/#1/D.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <stack> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <deque> 19#include <ctime> 20#define fst first 21#define sec second 22#define lson l,m,rt<<1 23#define rson m+1,r,rt<<1|1 24#define ms(a,x) memset(a,x,sizeof(a)) 25typedef long long LL; 26#define pi pair < int ,int > 27#define MP make_pair 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=280; 34int n,m; 35int total,adva,advb; 36struct Friend 37{ 38 int money; 39 int adv; 40}f[N]; 41struct Room 42{ 43 int type; 44 int cost; 45 int adv; 46}r[N]; 47struct Ans 48{ 49 int val; 50 int rid; 51 int fid; 52 bool operator < (Ans b)const 53 { 54 return val>b.val; 55 } 56}ans[300*300]; 57int main() 58{ 59 #ifndef ONLINE_JUDGE 60 freopen("code/in.txt","r",stdin); 61 #endif 62 cin>>total>>adva>>advb; 63 cin>>n; 64 for ( int i = 1 ; i <= n ; i++) 65 scanf("%d %d",&f[i].money,&f[i].adv); 66 scanf("%d",&m); 67 for ( int i = 1 ; i <= m ; i++) 68 scanf("%d%d%d",&r[i].type,&r[i].cost,&r[i].adv); 69 int cnt = 0 ; 70 for ( int i = 1 ; i <= m ; i++) 71 { 72 if (r[i].type==1) 73 { 74 if (r[i].cost<=total) 75 { 76 cnt++; 77 ans[cnt].val = r[i].adv+adva; 78 ans[cnt].rid = i; 79 ans[cnt].fid = -1; 80 } 81 continue; 82 } 83 else 84 { 85 for ( int j = 0 ; j <= n ; j++) 86 { 87 if (j==0) //自己住双人间 88 { 89 if (r[i].cost<=total) 90 { 91 cnt++; 92 ans[cnt].val = r[i].adv+advb; 93 ans[cnt].rid = i ; 94 ans[cnt].fid = -1; 95 } 96 } 97 else 98 { 99 if (r[i].cost<=total*2&&r[i].cost<=f[j].money*2) 100 { 101 cnt++; 102 ans[cnt].val = r[i].adv+f[j].adv; 103 ans[cnt].rid = i ; 104 ans[cnt].fid = j; 105 } 106 } 107 } 108 } 109 } 110// for ( int i = 1 ; i <= cnt ; i++) 111// { 112// printf("val:%d room: %d friend : %d \n",ans[i].val,ans[i].rid,ans[i].fid); 113// } 114 if (cnt==0) 115 { 116 puts("Forget about apartments. Live in the dormitory."); 117 }else 118 { 119 sort(ans+1,ans+cnt+1); 120 if (r[ans[1].rid].type==1) 121 { 122 printf("You should rent the apartment #%d alone.\n",ans[1].rid); 123 } 124 else 125 { 126 if (ans[1].fid==-1) 127 { 128 printf("You should rent the apartment #%d alone.\n",ans[1].rid); 129 } 130 else 131 { 132 133 printf("You should rent the apartment #%d with the friend #%d.\n",ans[1].rid,ans[1].fid); 134 135 } 136 } 137 } 138 #ifndef ONLINE_JUDGE 139 fclose(stdin); 140 #endif 141 return 0; 142}

BZOJ 1640/1692 : [Usaco2007 Nov]Best Cow Line 队列变换 (贪心)

·1568 words·4 mins
1640: [Usaco2007 Nov]Best Cow Line 队列变换 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 710 Solved: 373 [Submit][Status][Discuss] Description # FJ打算带着他可爱的N (1 ≤ N ≤ 2,000)头奶牛去参加“年度最佳老农”的比赛。在比赛中,每个农夫把他的奶牛排成一列,然后准备经过评委检验。比赛中简单地将奶牛的名字缩写为其头字母(the initial letter of every cow),举个例子,FJ带了Bessie, Sylvia,和Dora,那么就可以缩写为BSD. FJ只需将奶牛的一个序列重新排列,然后参加比赛。他可以让序列中的第一头奶牛,或者最后一头走出来,站到新队列的队尾。利欲熏心的FJ为了取得冠军,他就必须使新队列的字典序尽量小。给你初始奶牛序列(用头字母)表示,然后按照上述的规则组成新序列,并使新序列的字典序尽量小。

BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花(贪心)

·864 words·2 mins
1634: [Usaco2007 Jan]Protecting the Flowers 护花 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 605 Solved: 383 [Submit][Status][Discuss] Description # Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cows were in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport the cows back to their barn. Each cow i is at a location that is Ti minutes (1 <= Ti <= 2,000,000) away from the barn. Furthermore, while waiting for transport, she destroys Di (1 <= Di <= 100) flowers per minute. No matter how hard he tries,FJ can only transport one cow at a time back to the barn. Moving cow i to the barn requires 2*Ti minutes (Ti to get there and Ti to return). Write a program to determine the order in which FJ should pick up the cows so that the total number of flowers destroyed is minimized.