Codeforces Round #697 (Div. 3) D题 Cleaning the Phone 和 E题 Advertising Agency

D题考察的是贪心,注意用快读,用cin超时。

//Dlove's template
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define R register
#define ll long long
#define ull unsigned long long
#define db double
#define ld long double
#define Ls root << 1
#define Rs Ls | 1
#define sqr(_x) ((_x) * (_x))
#define Cmax(_a, _b) ((_a) < (_b) ? (_a) = (_b), 1 : 0)
#define Cmin(_a, _b) ((_a) > (_b) ? (_a) = (_b), 1 : 0)
#define Max(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define Min(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define Abs(_x) (_x < 0 ? (-(_x)) : (_x))

using namespace std;

//#define getchar() (_S == _T && (_T = (_S =_B) + fread(_B, 1, 1 << 15, stdin), _S == _T) ? 0 : *_S++)
//char _B[1 << 15], *_S = _B, *_T = _B;

inline int read()
{
 R int a = 0, b = 1; R char c = getchar();
 for(; c < '0' || c > '9'; c = getchar()) (c == '-') ? b = -1 : 0;
 for(; c >= '0' && c <= '9'; c = getchar()) a = (a << 1) + (a << 3) + c - '0';
 return a * b;
}
inline ll lread()
{
 R ll a = 0, b = 1; R char c = getchar();
 for(; c < '0' || c > '9'; c = getchar()) (c == '-') ? b = -1 : 0;
 for(; c >= '0' && c <= '9'; c = getchar()) a = (a << 1) + (a << 3) + c - '0';
 return a * b;
}

const int maxn = 200010;
int a[maxn];
int b[maxn];
/*priority_queue<double> p;
priority_queue<double> q;
priority_queue<pair<double,int> > r;*/
double l[maxn];
double r[maxn];
pair<double,int> p[maxn];

int main()
{
	int t;
	cin >> t;
	while(t--){
		int n,m;
		n = read();
		m = read();
		long long ans = 0;
		for(int i = 0;i<n;i++){
			scanf("%d",&a[i]);
			ans += a[i];
		}
		int j1 = 0;
		int j2 = 0;
		int j = 0;
		for(int i = 0;i<n;i++){
			b[i] = read();
			p[++j] = make_pair((double)a[i]/(double)b[i],i);
			if(b[i] == 1){
				l[++j1] = (double)a[i];
			}
			else {
				r[++j2] = (double)a[i];
			}
		}
		if(ans<m){
			cout << "-1" << endl;
		}
        else {
        	sort(l,l+j1+1);
        	sort(r,r+j2+1);
        	sort(p,p+j+1);
	        int cnt = 0;
			int sum = 0;
			int v = 0;
			while(j>0){
				//int x = r.top().first;
				int y = p[j].second;
				if(sum+a[y] < m){
					sum += a[y];
					j--;
					cnt += b[y];
					if(b[y] == 1) {
						j1--;
						v = a[y];
				    }
					else j2--;
				}
				else {
					if(b[y] == 1){
						cnt++;
					    sum += a[y];
					    v = a[y];
						break;
					}
					else {
						if(j1>0&& sum+l[j1] >= m){
							cnt++;
							sum += l[j1];
							v = l[j1];
						}
						else {
							sum += a[y];
							cnt += 2;
						}
						break;
					}
				}
				if(sum >= m) break;
			}
			if(v != 0&&sum-v >= m) cnt--;
			cout << cnt << endl;
		}
		/*while(!p.empty()){
			p.pop();
		}
		while(!q.empty()){
			q.pop();
		}
		while(!r.empty()){
			r.pop();
		}*/
	}
// printf("%d\n", read() + read());
 return 0;
}

/*
   动态排序用优先级队列
   静态排序用sort就行
   静态的优先级队列等于排好序的数组 
*/ 

E题考察的是逆元,组合数,贪心。

//Dlove's template
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define R register
#define ll long long
#define ull unsigned long long
#define db double
#define ld long double
#define Ls root << 1
#define Rs Ls | 1
#define sqr(_x) ((_x) * (_x))
#define Cmax(_a, _b) ((_a) < (_b) ? (_a) = (_b), 1 : 0)
#define Cmin(_a, _b) ((_a) > (_b) ? (_a) = (_b), 1 : 0)
#define Max(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define Min(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define Abs(_x) (_x < 0 ? (-(_x)) : (_x))

using namespace std;

//#define getchar() (_S == _T && (_T = (_S =_B) + fread(_B, 1, 1 << 15, stdin), _S == _T) ? 0 : *_S++)
//char _B[1 << 15], *_S = _B, *_T = _B;

inline int read()
{
 R int a = 0, b = 1; R char c = getchar();
 for(; c < '0' || c > '9'; c = getchar()) (c == '-') ? b = -1 : 0;
 for(; c >= '0' && c <= '9'; c = getchar()) a = (a << 1) + (a << 3) + c - '0';
 return a * b;
}
inline ll lread()
{
 R ll a = 0, b = 1; R char c = getchar();
 for(; c < '0' || c > '9'; c = getchar()) (c == '-') ? b = -1 : 0;
 for(; c >= '0' && c <= '9'; c = getchar()) a = (a << 1) + (a << 3) + c - '0';
 return a * b;
}

const int mod = 1e9+7;
const int maxn = 1010;
int a[maxn];
map<int,int> mp;

void exgcd(int a,int b,int &x,int &y){
	if(!b){
		x = 1;
		y = 0;
		return ;
	}
	exgcd(b,a%b,y,x);
	y -= a/b*x;
}

int main()
{
	int t;
	t = read();
	while(t--){
		int n,k;
		n = read(),k = read();
		for(int i = 0;i<n;i++){
			cin >> a[i];
		}
		//sort(a,a+n);
		long long ans = 1;
		for(int i = 0;i<n;i++){
			int x = a[i];
			//if(mp.find(x) == mp.end()) mp.insert(make_pair(x,1));
			//else mp[x]++;
			mp[-x]++;
		}
		int sum = 0;
		map<int,int>::iterator it;
		for(it = mp.begin() ;it != mp.end() ;it++){
			int x = it->second;
			if(sum+x <= k) sum += x;
			else {
				int y = k-sum;
				for(int i = x;i>x-y;i--){
					ans = (ans*i)%mod;
				}
				for(int i = 1;i <= y;i++){
					int u,v;
					exgcd(i,mod,u,v);
					u = (u+mod)%mod;
					ans = (ans*u)%mod;
					//cout << u << endl;
				}
				sum = k;
			}
			if(sum == k) break;
		}
		cout << (ans+mod)%mod << endl;
		mp.clear();
	} 
// printf("%d\n", read() + read());
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值