a-cable master
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to connect computers for the contestants using a "star" topology - i.e. connect them all to a single central hub. To organize a truly honest contest, the Head of the Judging Committee has decreed to place all contestants evenly around the hub on an equal distance from it.
To buy network cables, the Judging Committee has contacted a local network solutions provider with a request to sell for them a specified number of cables with equal lengths. The Judging Committee wants the cables to be as long as possible to sit contestants as far from each other as possible.
The Cable Master of the company was assigned to the task. He knows the length of each cable in the stock up to a centimeter,and he can cut them with a centimeter precision being told the length of the pieces he must cut. However, this time, the length is not known and the Cable Master is completely puzzled.
You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.
Input
The first line of the input file contains two integer numb ers N and K, separated by a space. N (1 = N = 10000) is the number of cables in the stock, and K (1 = K = 10000) is the number of requested pieces. The first line is followed by N lines with one number per line, that specify the length of each cable in the stock in meters. All cables are at least 1 meter and at most 100 kilometers in length. All lengths in the input file are written with a centimeter precision, with exactly two digits after a decimal point.
Output
Write to the output file the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly two digits after a decimal point.
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output file must contain the single number "0.00" (without quotes).
Sample Input
4 11
8.02
7.43
4.57
5.39
Sample Output
2.00
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
const int maxn=1e4+10;
const double li=1e-8;
double a[maxn];
ll n,k;
bool check(double x){
ll res=0;
// cout<<k<<"ff"<<endl;
for(ll i=1;i<=n;i++){
res+=(ll)(a[i]/x);
// cout<<x<<"f"<<(int)(a[i]/x)<<endl;
}
if(res>=k) return true;
return false;
}
int main(){
// printf("%.2lf",0.005);
scanf("%lld%lld",&n,&k);
//{
double mi=1e9;double ma=0;
for(int i=1;i<=n;i++){
double x;
scanf("%lf",&a[i]);
a[i]+=0.005;
// a[i]=(x*10)/10;
//cout<<a[i]<<"f"<<endl;
if(mi>a[i]) mi=a[i];
if(ma<a[i]) ma=a[i];
}
// cout<<mi<<" "<<ma<<endl;
//int mid=(mi+ma)/2;
double l=0;double r=ma;
double mid;
//cout<<li*10000<<endl;
while((r-l)>li){
mid=(l+r)/2;
//cout<<mid<<endl;
if(check(mid)){
l=mid;
// cout<<l<<endl;
}
else {
r=mid;
}
}
// l=0.005;
if(l<0.01) cout<<"0.00"<<endl;
else
//cout<<l<<endl;
//double ans=mid;
printf("%.2f\n",double(int(l*100)*1.0/100));
//}
return 0;
}
这题目看起来很鬼畜啊,我太难了。重要的是小数二分很正常叭,然后,至于,不要随便乱用cout之类的?
至于为什么加上0.005,因为可能损失精度******很重要的
b-agressive cows
Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).
His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
Input
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
* Line 1: One integer: the largest minimum distance
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e5+10;
int a[maxn];int n,k;
int b[maxn];
bool check(int x){
int chu=b[1];int end;int res=1;
//cout<<k<<endl;
for(int i=2;i<=n;i++){
if(b[i]-chu>=x){
res++;
chu=b[i];
}
}
//cout<<x<<" "<<res<<endl;
if(res>=k) return true;
return false;
}
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+1,a+n+1);b[0]=0;
for(int i=1;i<=n;i++){
b[i]=a[i];
}
int l=0;int r=b[n];int mid;
while(l+1<r){
mid=(l+r)/2;
if(check(mid)){
l=mid;
}
else r=mid;
}
printf("%d\n",l);
return 0;
}
重要的是思路。二分,基础是有序
C-river hopscotch
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Line 1: Three space-separated integers: L, N, and M
Lines 2.. N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=5e4+10;
int a[maxn];int n,k;int c;int l;
int b[maxn];
bool check(int x){
int chu=b[0];int end;int res=0;
//cout<<k<<endl;
for(int i=1;i<=n+1;i++){
if(b[i]-chu>x){
//res++;
chu=b[i];
//cout<<chu<<"chu"<<endl;
}
else res++;
}
//cout<<x<<" "<<res<<" "<<k<<endl;
//res=n+2-res;
if(res<=k){
// cout<<"bb"<<endl;
return true;
}
return false;
}
int main(){
//int c;
while(scanf("%d%d%d",&l,&n,&k)!=EOF){
// cout<<c<<"f"<<endl;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+1,a+n+1);b[0]=0;
for(int i=1;i<=n;i++){
b[i]=a[i];
}
// for(int i=1;i<=n;i++){
// cout<<b[i]<<"wer"<<endl;
// }
b[n+1]=l;
int l=0;int r=b[n+1];int mid;
while(l<=r){
mid=(l+r)/2;
// cout<<mid<<endl;
if(check(mid)){
l=mid+1;
}
else r=mid-1;
}
printf("%d\n",l);
}
return 0;
}
重要的是思路,还有很难找到的bug。思路至少保证理论上可做,所以努力做吖,基本上没有很难找到的bug叭
所以思路很重要的
这个好像是复制粘贴前面的,还是 要认真写的呢,orz
d-monthly expense
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.
FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.
Input
Line 1: Two space-separated integers: N and M
Lines 2.. N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
#define ll long long
const int maxn=1e5+10;
int a[maxn];
bool judge(ll x){
ll chu=0;int num=0;
for(int i=1;i<=n;i++){
if(chu+a[i]>x){
num++;
chu=a[i];
}
else chu=chu+a[i];
}
num++;
if(num>m){
return true;
}
return false;
}
int main(){
scanf("%d%d",&n,&m);
int ma=0;int mi=10000;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(mi>a[i]) mi=a[i];
if(ma<a[i]) ma=a[i];
}
ll l=ma;ll r=ma*n;
//cout<<r<<endl;
while(l<r){
ll mid=(l+r)/2;
// cout<<mid<<endl;
if(judge(mid)){
l=mid+1;
}
else r=mid;
}
cout<<r<<endl;
return 0;
}
还有尾巴,num++,这个稍微值得注意,很容易wa 很多次的,然后,还有二分的变形
e-drying
It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.
Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.
There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.
Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).
The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.
Input
The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).
Output
Output a single integer — the minimal possible number of minutes required to dry all clothes.
Sample Input
sample input #1
3
2 3 9
5
sample input #2
3
2 3 6
5
Sample Output
sample output #1
3
sample output #2
2
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
ll n,m;
const ll maxn=1e5+10;
ll a[maxn];
bool judge(ll x){
ll chu=0;
if(m==1) return true;//特判,最主要是分母可能为0
for(ll i=1;i<=n;i++){
if(a[i]>x){
chu+=(a[i]-x+m-2)/(m-1);//
// cout<<chu<<"chu"<<endl;
}
}
//多出来的是用机器的时间
// cout<<"end"<<endl;
if(chu>x){//
return true;
}
return false;
}
int main(){
//while(scanf("%lld",&n)!=EOF){
cin>>n;
ll ma=0;ll mi=1e9+10;
for(ll i=1;i<=n;i++){
scanf("%d",&a[i]);
if(mi>a[i]) mi=a[i];
if(ma<a[i]) ma=a[i];
}
scanf("%lld",&m);
ll l=0;ll r=ma;//用最保险的叭
// cout<<l<<" "<<r<<endl;
while(l+1<r){
ll mid=(l+r)/2;
//cout<<mid<<endl;
if(judge(mid)){
l=mid;
//ans=mid;
// cout<<ans<<"ans"<<endl;
}
else r=mid;
}
cout<<l+1<<endl;//取右端点的原因是 tongyi,还可能l==r
//}
return 0;
}
二分对象还是挺重要的,二分的是使用吹风机时间。
设某次二分出的一个值是mid:
1、对于一件ai值小于等于mid的衣服,直接晾干即可;
2、对于一件ai值大于mid值的衣服,最少的用时是用机器一段时间,晾干一段时间,设这两段时间分别是x1和x2,那么有mid=x1+x2,ai<=k*x1+x2,解得x1>=(ai-mid)/(k-1) ,所以对(ai-mid)/(k-1)向上取整就是该件衣服的最少用时。
原文链接:https://blog.csdn.net/zwj1452267376/article/details/50248393
f-cow acrobats
Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met with a dismal failure). Thus, they have decided to practice performing acrobatic stunts.
The cows aren't terribly creative and have only come up with one acrobatic stunt: standing on top of each other to form a vertical stack of some height. The cows are trying to figure out the order in which they should arrange themselves ithin this stack.
Each of the N cows has an associated weight (1 <= W_i <= 10,000) and strength (1 <= S_i <= 1,000,000,000). The risk of a cow collapsing is equal to the combined weight of all cows on top of her (not including her own weight, of course) minus her strength (so that a stronger cow has a lower risk). Your task is to determine an ordering of the cows that minimizes the greatest risk of collapse for any of the cows.
Input
* Line 1: A single line with the integer N.
* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.
Output
* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.
Sample Input
3
10 3
2 5
3 3
Sample Output
2
Hint
OUTPUT DETAILS:
Put the cow with weight 10 on the bottom. She will carry the other two cows, so the risk of her collapsing is 2+3-3=2. The other cows have lower risk of collapsing.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
const int maxn=5e4+10;
struct node{
int l,r;
}nod[maxn];
bool cmp(node a,node b){
return a.l+a.r<b.l+b.r;//倒推
}
int a[maxn];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&nod[i].l,&nod[i].r);
a[i]=a[i-1]+nod[i].l;
}
sort(nod+1,nod+n+1,cmp);
int sum=a[n];int mi=-1e9;
for(int i=n;i>=1;i--){
int res=sum-nod[i].r-nod[i].l;//第二个思路,可是你怎么知道b的危险指数是在那一堆最大//
sum=sum-nod[i].l;//
if(mi<res) mi=res;//要找最大,怪不得又错了
}
cout<<mi<<endl;//贪心思想我觉得不太对
return 0;
}
好像似乎贪心我不是很懂的,总觉得这个思路有问题,因为数据水,水过去了罢了
第一个和倒数第二个比较是最优的,然后第二个和倒数第三个比较比较,也是最优的,那么最后一个,和倒数第三个比也是最优的排列,这个贪心思想是可行的。传递性很重要。
至于是否整体取最优就是取决于传递性。
当然贪心也可以试一波?
1:对于每头牛而言,视他与他上面的牛为一个整体,总重为sum_w,那么对于这头牛的危险值为sum_w-w-s,要想sum_w-(w+s)的值最小,w+s的值就应该最大,由此可见取最优化,wi+si越大越应该在下面。
2:假设目前排放的的序列是最优排列。任意位置有第一头牛和第二头牛分别有w1,s1;w2,s2。第一头牛上面的牛的重量总和为sum。且第一头牛在第二头牛上面,可以知道危险指数分别为a=sum-s1,b=sum+w1-s2。现在调换两头牛的位置,可得a'=sum+w2-s1,b'=sum-s2。 因为之前是最优排列,所以得知w2-s1>=w1-s2, 移项可得:
w2+s2>=w1+s1,所以体重和力量之和越大越在底下。
原文链接:https://blog.csdn.net/zwj1452267376/article/details/50269771
第二个倒是比较容易想到,第一个有些构造性。
g-dropping tests
| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
|
Language:Default Dropping tests
Description In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cumulative average is defined to be . Given your test scores and a positive integer k, determine how high you can make your cumulative average if you are allowed to drop any k of your test scores. Suppose you take 3 tests with scores of 5/5, 0/1, and 2/6. Without dropping any tests, your cumulative average is . However, if you drop the third test, your cumulative average becomes . Input The input test file will contain multiple test cases, each containing exactly three lines. The first line contains two integers, 1 ≤ n ≤ 1000 and 0 ≤ k < n. The second line contains n integers indicating ai for all i. The third line contains n positive integers indicating bi for all i. It is guaranteed that 0 ≤ ai ≤ bi ≤ 1, 000, 000, 000. The end-of-file is marked by a test case with n = k = 0 and should not be processed. Output For each test case, write a single line with the highest cumulative average possible after dropping k of the given test scores. The average should be rounded to the nearest integer. Sample Input Sample Output Hint To avoid ambiguities due to rounding errors, the judge tests have been constructed so that all answers are at least 0.001 away from a decision boundary (i.e., you can assume that the average is never 83.4997). Source |
[Submit] [Go Back] [Status] [Discuss]
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const ll maxn=1001;
double a[maxn];
double b[maxn];
double c[maxn];
ll n,m;
bool cmp(double aa,double b){
return aa>b;
}
double sos(double x){
double res=0;
//sort()
for(ll i=1;i<=n;i++){
c[i]=100*(a[i])-x*b[i];
}
sort(c+1,c+n+1,cmp);
for(ll i=1;i<=n-m;i++){
res+=c[i];
}
return res;
}
void solve(){
double l=0;double r=100+3;//
while(r-l>0.001){
double mid=(l+r)/2;
if(sos(mid)>=0){
l=mid;
}
else {
r=mid;
}
}
cout<<int(l+0.5)<<endl;
}
int main(){
while(scanf("%lld%lld",&n,&m)!=EOF&&(n+m)){
for(ll i=1;i<=n;i++){
scanf("%lf",&a[i]);
}
for(ll i=1;i<=n;i++){
scanf("%lf",&b[i]);
}
solve();
}
return 0;
}
重要的是它线性变化
建立一个等式,然后就就就酱紫了
要最大的x,那么就要从大到小排,使0的位置往右移
h-k best
Language:Default K Best
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keep k best jewels for herself. She decided to keep such jewels that their specific value is as large as possible. That is, denote the specific value of some set of jewels S = {i1, i2, …, ik} as . Demy would like to select such k jewels that their specific value is maximal possible. Help her to do so. Input The first line of the input file contains n — the number of jewels Demy got, and k — the number of jewels she would like to keep (1 ≤ k ≤ n ≤ 100 000). The following n lines contain two integer numbers each — vi and wi (0 ≤ vi ≤ 106, 1 ≤ wi ≤ 106, both the sum of all vi and the sum of all wi do not exceed 107). Output Output k numbers — the numbers of jewels Demy must keep. If there are several solutions, output any one. Sample Input 3 2 1 1 1 2 1 3 Sample Output 1 2 |
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const ll maxn=1e5+10;
ll a[maxn];
ll b[maxn];
double c[maxn];
struct node{
double l;int r;
}nod[maxn];
ll n,m;
bool cmp(double aa,double b){
return aa>b;
}
bool cmp2(node aa,node bb){
return aa.l>bb.l;
}
double sos(double x){
double res=0;
//sort()
for(ll i=1;i<=n;i++){
nod[i].l=(a[i])*1.0-x*b[i]*1.0;
nod[i].r=i;
}
sort(nod+1,nod+n+1,cmp2);
for(ll i=1;i<=m;i++){
res+=nod[i].l;
}
return res;
}
void solve(){
double l=0;double r=1e9;//
while(r-l>1e-8){
double mid=(l+r)/2;
if(sos(mid)>=0){
l=mid;
}
else {
r=mid;
}
}
for(ll i=1;i<=m;i++){
cout<<nod[i].r<<" ";
}
cout<<endl;
//cout<<int(l+0.5)<<endl;
}
int main(){
while(scanf("%lld%lld",&n,&m)!=EOF){
// scanf("%lld%lld",&n,&m);
for(ll i=1;i<=n;i++){
scanf("%lld%lld",&a[i],&b[i]);
}
/*for(ll i=1;i<=n;i++){
scanf("%lf",&b[i]);
}*/
solve();
}
return 0;
}
就是多了点输出序号而已,我弱智了
i-median
Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numbers: ∣Xi - Xj∣ (1 ≤ i < j ≤ N). We can get C(N,2) differences through this work, and now your task is to find the median of the differences as quickly as you can!
Note in this problem, the median is defined as the (m/2)-th smallest number if m,the amount of the differences, is even. For example, you have to find the third smallest one in the case of m = 6.
Input
The input consists of several test cases.
In each test case, N will be given in the first line. Then N numbers are given, representing X1, X2, ... , XN, ( Xi ≤ 1,000,000,000 3 ≤ N ≤ 1,00,000 )
Output
For each test case, output the median in a separate line.
Sample Input
4 1 3 2 4 3 1 10 2
Sample Output
1 8
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e5+10;
int a[maxn];int n;int m;
bool judge(int x){
int cnt=0;
for(int i=0;i<n;i++){
int t=upper_bound(a,a+n,a[i]+x)-a;//要大于才行******
cnt+=(t-(i+1));//
}
if(cnt>=m){
return true;
}
return false;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
//for(int i=1;i<=)
m=(n-1)*n/2;
m=(m+1)/2;
int l=0;int r=a[n-1]-a[0];
int ans=0;
while(l<=r){
int mid=(l+r)/2;
if(judge(mid)){
ans=mid;
r=mid-1;
}
else l=mid+1;
}
cout<<ans<<endl;
}return 0;
}
必须得大于的原因,在于能取到最终准确答案。易错
二分求差值的和,第几大
j-matrix
Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i2 + 100000 × i + j2 - 100000 × j + i × j, you are to find the M-th smallest element in the matrix.
Input
The first line of input is the number of test case.
For each test case there is only one line contains two integers, N(1 ≤ N ≤ 50,000) and M(1 ≤ M ≤ N × N). There is a blank line before each test case.
Output
For each test case output the answer on a single line.
Sample Input
12 1 1 2 1 2 2 2 3 2 4 3 1 3 2 3 8 3 9 5 1 5 25 5 10
Sample Output
3 -99993 3 12 100007 -199987 -99993 100019 200013 -399969 400031 -99939
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=5e4+10;
ll n,m;
ll calc(ll i,ll j){
return (i*i + 100000*i + j*j-100000*j + i*j);
}
ll ji(ll j,ll value){
ll l=0;ll r=n+1;//
while(l+1<r){
ll mid=(l+r)>>1;
if(calc(mid,j)>=value){
r=mid;
}
else l=mid;
}
return r-1;//
}
ll hand(ll num){
ll cnt=0;
for(ll i=1;i<=n;i++){
cnt+=ji(i,num);
}
return cnt;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
//ll n,m;
scanf("%lld%lld",&n,&m);
ll l=-1e10; ll r=1e10+3;
while(l+1<r){
ll mid=(l+r)>>1;
if(hand(mid)>=m){
r=mid;
}
else l=mid;
}
cout<<r-1<<endl;//
}
return 0;
}
根据式子的单调性,然后两次二分,注意细节(取不到二分的数值
重要的是两次二分nb
m-moo university -financial aid
Bessie noted that although humans have many universities they can attend, cows have none. To remedy this problem, she and her fellow cows formed a new university called The University of Wisconsin-Farmside,"Moo U" for short.
Not wishing to admit dumber-than-average cows, the founders created an incredibly precise admission exam called the Cow Scholastic Aptitude Test (CSAT) that yields scores in the range 1..2,000,000,000.
Moo U is very expensive to attend; not all calves can afford it.In fact, most calves need some sort of financial aid (0 <= aid <=100,000). The government does not provide scholarships to calves,so all the money must come from the university's limited fund (whose total money is F, 0 <= F <= 2,000,000,000).
Worse still, Moo U only has classrooms for an odd number N (1 <= N <= 19,999) of the C (N <= C <= 100,000) calves who have applied.Bessie wants to admit exactly N calves in order to maximize educational opportunity. She still wants the median CSAT score of the admitted calves to be as high as possible.
Recall that the median of a set of integers whose size is odd is the middle value when they are sorted. For example, the median of the set {3, 8, 9, 7, 5} is 7, as there are exactly two values above 7 and exactly two values below it.
Given the score and required financial aid for each calf that applies, the total number of calves to accept, and the total amount of money Bessie has for financial aid, determine the maximum median score Bessie can obtain by carefully admitting an optimal set of calves.
Input
* Line 1: Three space-separated integers N, C, and F
* Lines 2..C+1: Two space-separated integers per line. The first is the calf's CSAT score; the second integer is the required amount of financial aid the calf needs
Output
* Line 1: A single integer, the maximum median score that Bessie can achieve. If there is insufficient money to admit N calves,output -1.
Sample Input
3 5 70 30 25 50 21 20 20 5 18 35 30
Sample Output
35
Hint
Sample output:If Bessie accepts the calves with CSAT scores of 5, 35, and 50, the median is 35. The total financial aid required is 18 + 30 + 21 = 69 <= 70.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
#define ll long long
const int maxn=1e5+10;
ll c,n,f;
struct node{
ll scr,money;
bool operator < (const node &a) const {
return a.scr>scr;//要加个Return 啊
}
}nod[maxn];
ll pre_sum[maxn]; ll end_sum[maxn];
void ge(){
c=c/2;
ll sum=0;
priority_queue<int> qu;//youxianduilie
for(int i=1;i<=c;i++){
sum+=nod[i].money;
qu.push(nod[i].money);
pre_sum[i]=sum;
}
for(int i=c+1;i<=n;i++){
int q=qu.top();//top
if(q>nod[i].money){//不可取等号
qu.pop();
qu.push(nod[i].money);
sum-=q-nod[i].money;
}
pre_sum[i]=sum;
}
sum=0;
while(!qu.empty()) qu.pop();
for(int i=n;i>n-c;i--){
sum+=nod[i].money;
qu.push(nod[i].money);
end_sum[i]=sum;
}
for(int i=n-c;i>=1;i--){
int q=qu.top();
if(q>=nod[i].money){//在钱相等的时候,分数要尽可能大
qu.pop();
qu.push(nod[i].money);
sum-=q-nod[i].money;//
}
end_sum[i]=sum;
}
}
ll get_sum(){
for(int i=n-c;i>c;i--){
ll sum=pre_sum[i-1]+end_sum[i+1];
sum+=nod[i].money;
if(sum<=f){
//cout<<i<<"wo"<<nod[i].scr<<endl;
return nod[i].scr;//尽量最大
}
}
return -1;
}
int main(){
scanf("%lld%lld%lld",&c,&n,&f);
for(int i=1;i<=n;i++){
scanf("%lld%lld",&nod[i].scr,&nod[i].money);
}
sort(nod+1,nod+n+1);
ge();
cout<<get_sum()<<endl;
return 0;
}
贪心,注意细节就很容易读懂代码,应该
本身应该按照分数降序排列,然后从后往前扫一遍就是了
前面C/2和后面c/2预处理,用优先队列
前缀和的预处理,和后缀和的预处理很棒啊
t-telephone lines
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.
There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John's property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.
The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used.
As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.
Determine the minimum amount that Farmer John must pay.
Input
* Line 1: Three space-separated integers: N, P, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: Ai, Bi, and Li
Output
* Line 1: A single integer, the minimum amount Farmer John can pay. If it is impossible to connect the farm to the phone company, print -1.
Sample Input
5 7 1 1 2 5 3 1 4 2 4 8 3 2 3 5 2 9 3 4 7 4 5 6
Sample Output
4
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1010;
int tot;
int head[maxn];
int dis[maxn];
bool vis[maxn];
const int inf=0x3f3f3f3f;
struct node{
int v,w,c,next;
}nod[maxn*20];
int n,m;int k;
int q[maxn];
void addedge(int u,int v,int w){
nod[tot].v=v;
nod[tot].w=w;
nod[tot].next=head[u];
head[u]=tot++;
}
int judge(int x){
for(int i=1;i<=n;i++){
for(int j=head[i];j!=-1;j=nod[j].next){
if(nod[j].w<=x){
nod[j].c=0;
}
else nod[j].c=1;
}
}
int be=0,en=0;
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[1]=0;
vis[1]=1;
q[++en]=1;
while(be!=en){
int to=q[be=(be+1)%maxn];//取余否则可能RE
vis[to]=0;
for(int i=head[to];i!=-1;i=nod[i].next){
int v=nod[i].v;
if(dis[v]>dis[to]+nod[i].c){
dis[v]=dis[to]+nod[i].c;
if(!vis[v]){
vis[v]=1;
q[en=(en+1)%maxn]=v;
}
}
}
}
return dis[n]<=k;
}
int main(){
while( scanf("%d%d%d",&n,&m,&k)!=EOF){
int a,b,ll;
int l=0,r=0;
tot=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++){
scanf("%d%d%d",&a,&b,&ll);
addedge(a,b,ll);
addedge(b,a,ll);
r=max(r,ll);
}
//int mid=(l+r)>>1;
int ans=-1;
while(l<=r){
int mid=(l+r)>>1;
if(judge(mid)){
ans=mid;
r=mid-1;
}
else {
l=mid+1;
}
}
cout<<ans<<endl;
}
return 0;
}
二分加上最短路,单源,长度可以映射为0和1,表示用和不用
m-garland
The New Year garland consists of N lamps attached to a common wire that hangs down on the ends to which outermost lamps are affixed. The wire sags under the weight of lamp in a particular way: each lamp is hanging at the height that is 1 millimeter lower than the average height of the two adjacent lamps.
The leftmost lamp in hanging at the height of A millimeters above the ground. You have to determine the lowest height B of the rightmost lamp so that no lamp in the garland lies on the ground though some of them may touch the ground.
You shall neglect the lamp's size in this problem. By numbering the lamps with integers from 1 to N and denoting the ith lamp height in millimeters as Hi we derive the following equations:
H1 = A
Hi = (H i-1 + H i+1)/2 - 1, for all 1 < i < N
HN = B
Hi >= 0, for all 1 <= i <= N
The sample garland with 8 lamps that is shown on the picture has A = 15 and B = 9.75.
Input
The input file consists of a single line with two numbers N and A separated by a space. N (3 <= N <= 1000) is an integer representing the number of lamps in the garland, A (10 <= A <= 1000) is a real number representing the height of the leftmost lamp above the ground in millimeters.
Output
Write to the output file the single real number B accurate to two digits to the right of the decimal point representing the lowest possible height of the rightmost lamp.
Sample Input
692 532.81
Sample Output
446113.34
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int n;double a;double b;
const int maxn=1e3+10;
double f[maxn];
bool judge(double x){
f[1]=a;
f[2]=x;
for(int i=3;i<=n;i++){
f[i]=2*f[i-1]-f[i-2]+2;
if(f[i]<0) return false;
}
b=f[n];
return 1;
}
int main(){
// f[1]=a;
while(scanf("%d%lf",&n,&a)!=EOF){
double l=-1;
double r=maxn;
for(int i=1;i<=1100;i++){
double mid=(l+r)*1.0/2;
if(judge(mid)){
r=mid;//不是整数
}
else l=mid;
}
printf("%.2f\n",b+1e-8);
}
return 0;
}
直接试
n-showstopper
Data-mining huge data sets can be a painful and long lasting process if we are not aware of tiny patterns existing within those data sets.
One reputable company has recently discovered a tiny bug in their hardware video processing solution and they are trying to create software workaround. To achieve maximum performance they use their chips in pairs and all data objects in memory should have even number of references. Under certain circumstances this rule became violated and exactly one data object is referred by odd number of references. They are ready to launch product and this is the only showstopper they have. They need YOU to help them resolve this critical issue in most efficient way.
Can you help them?
Input
Input file consists from multiple data sets separated by one or more empty lines.
Each data set represents a sequence of 32-bit (positive) integers (references) which are stored in compressed way.
Each line of input set consists from three single space separated 32-bit (positive) integers X Y Z and they represent following sequence of references: X, X+Z, X+2*Z, X+3*Z, …, X+K*Z, …(while (X+K*Z)<=Y).
Your task is to data-mine input data and for each set determine weather data were corrupted, which reference is occurring odd number of times, and count that reference.
Output
For each input data set you should print to standard output new line of text with either “no corruption” (low case) or two integers separated by single space (first one is reference that occurs odd number of times and second one is count of that reference).
Sample Input
1 10 1 2 10 1 1 10 1 1 10 1 1 10 1 4 4 1 1 5 1 6 10 1
Sample Output
1 1 no corruption 4 3
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=1e5+10;
ll x[maxn];
ll y[maxn];
ll z[maxn];
char str[maxn];
int cnt;
//ll sum;
ll check(ll mid){
ll sum=0;
for(int i=0;i<cnt;i++){
if(mid<x[i]) continue;
ll t=min(y[i],mid);//
sum+=(t-x[i])/z[i]+1;//
}
return sum;
}
void solve(){
cnt=0;
x[0]=0;
sscanf(str,"%lld%lld%lld",&x[cnt],&y[cnt],&z[cnt]);//
//cout<<x[cnt]<<endl;
cnt++;
if(!x[0]) return;
//gets(str);
//cout<<str<<endl;
while(gets(str)&&str[0])
{
//cout<<str<<endl;
sscanf(str,"%lld%lld%lld",&x[cnt],&y[cnt],&z[cnt]),cnt++;
//cout<<str<<"fe"<<endl;
}
// cout<<x[cnt]<<endl;
ll l=0; ll r=(1ll<<32);
while(l+1<r){//
ll mid=(l+r)/2;
//cout<<mid<<endl;
if(check(mid)%2){//
r=mid;//
}
else {
l=mid;
}
}
if(l+1==(1ll<<32)){//要加(ll)否则默认int会超出范围 ,不能加括号
cout<<"no corruption"<<endl;
}
else {
cout<<r<<" "<<check(r)-check(r-1)<<endl;
}
}
int main(){
while(gets(str)){//gets没有!=EOF
solve();
}
return 0;
}
注意使用二分,emmm
o-hiho drinking game
Little Hi and Little Ho are playing a drinking game called HIHO. The game comprises N rounds. Each round, Little Hi pours T milliliter of water into Little Ho's cup then Little Ho rolls a K-faces dice to get a random number d among 1 to K. If the remaining water in Little Ho's cup is less than or equal to d milliliter Little Hi gets one score and Little Ho drinks up the remaining water, otherwise Little Ho gets one score and Little Ho drinks exactly d milliliter of water from his cup. After N rounds who has the most scores wins.
Here comes the problem. If Little Ho can predict the number d of N rounds in the game what is the minimum value of T that makes Little Ho the winner? You may assume that no matter how much water is added, Little Ho's cup would never be full.
Input
The first line contains N(1 <= N <= 100000, N is odd) and K(1 <= K <= 100000).
The second line contains N numbers, Little Ho's predicted number d of N rounds.
Output
Output the minimum value of T that makes Little Ho the winner.
Sample Input
5 6 3 6 6 2 1
Sample Output
4
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=1e5+10;
ll a[maxn];
ll c[maxn];
ll n,k;ll t;
bool judge(int mid){
ll sum=0;
// cout<<mid<<endl;
ll win=0,lose=0;
for(int i=1;i<=n;i++){
sum+=mid;
if(sum<=a[i]) {
lose++;
sum=0;
}
else {
win++;
sum-=a[i];
}
}
// cout<<win<<" "<<lose<<"cnt"<<endl;
if(win>lose) return 1;
else return 0;
}
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
ll l=0;ll r=0x7f7f7f7f;//t=k;
while(l+1<r){
ll mid=(l+r)/2;
if(judge(mid)){
// cout<<mid<<endl;
t=mid;
r=mid;
}
else {
l=mid;
}
}
cout<<r<<endl;
return 0;
}
审题,主要是我自己写出来了,当时觉得还很弱智,结果现在基本上都忘完了,也就是自己写的也可以忘的干干净净的,至少是很难写出来的。
写博客很重要