链接:https://ac.nowcoder.com/acm/contest/91177/C
来源:牛客网
平面上有n个圆,小红可以进行任意次操作,每次操作可以选择一个圆,将它向任意方向移动若干距离。该操作的代价为该圆面积乘以移动的距离。
小红希望最终包含原点的圆数量不超过k,请你帮小红算出她操作的最小总代价。
输入描述:
第一行输入两个正整数n,k,用空格隔开。
接下来的n行,每行输入三个整数𝑥,𝑦,z
x,y,z,代表第i个圆的圆心是(x,y),半径是r。
1≤k≤n≤10e5
0≤x,y≤10e5
1≤r≤10e5
输出描述:
一个实数,代表操作的最小总代价。若你的输出和标准答案的相对误差不超过10的-6次方 ,则认为你的答案正确。
示例1
输入
2 1
0 0 1
0 0 2
输出
3.14159265358979324
说明
显然将小圆向任意方向移动距离1即可。
#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const double pi=3.14159265358979324;
const int N=1e5+10;
vector<double>minn;
int main()
{
int n,k,sum=0;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
double x,y,z;
cin>>x>>y>>z;
double d=x*x+y*y;
double s=pi*z*z;
if(d<=z*z) //记录包含原点的圆的个数以及需要移动的距离
{
minn.push_back((z-sqrt(d))*1.0*s);
sum++;
}
}
//注意题目要求的是不超过k
if(sum>k)
{
int num=sum-k;
sort(minn.begin(),minn.end());//排序
double ans=0;
for(int i=0;i<num;i++)
{
ans+=minn[i];
}
printf("%.7lf",ans);//精度
}
else cout<<0<<endl;
return 0;
}
INSERT INTO SC VALUES(‘S1’,’C1’,90);
INSERT INTO SC VALUES(‘S2’,’C2’,82);
INSERT INTO SC VALUES(‘S3’,’C1’,85);
INSERT INTO SC VALUES(‘S4’,’C1’,46);
INSERT INTO SC VALUES(‘S5’,’C2’,78);
INSERT INTO SC VALUES(‘S1’,’C2’,98);
INSERT INTO SC VALUES(‘S3’,’C2’,67);
INSERT INTO SC VALUES(‘S6’,’C2’,87);
INSERT INTO SC VALUES(‘S1’,’C3’,98);
INSERT INTO SC VALUES(‘S5’,’C3’,77);
INSERT INTO SC VALUES(‘S1’,’C4’,52);
INSERT INTO SC VALUES(‘S6’,’C4’,79);
INSERT INTO SC VALUES(‘S4’,’C2’,69);
INSERT INTO SC VALUES(‘S6’,’C3’,NULL);
INSERT INTO Teacher VALUES(‘T1’,’苏浩’,’教授’,‘2009-6-16’,’D1’);
INSERT INTO Teacher VALUES(‘T2’,’丁洁’,’讲师’,‘2015-7-13’,’D3’);
INSERT INTO Teacher VALUES(‘T3’,’唐海’,’讲师’,‘2010-7-4’,’D3’);
INSERT INTO Teacher VALUES(‘T4’,’邓晓’,’副教授’,‘2007-6-28’,’D1’);
INSERT INTO Teacher VALUES(‘T5’,’郑阳’,’助教’,‘2018-7-4’,’D2’);
INSERT INTO Teacher VALUES(‘T6’,’周伟’,’讲师’,‘2011-11-16’,NULL);
INSERT INTO TC(tno,cno,semester) VALUES(‘T1’,’C2’,‘2021-1’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T1’,’C1’,‘2021-2’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T2’,’C3’,‘2022-1’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T2’,’C3’,‘2020-2’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T4’,’C3’,‘2022-2’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T4’,’C4’,‘2022-1’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T5’,’C2’,‘2021-1’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T3’,’C3’,‘2020-2’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T1’,’C3’,‘2020-2’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T5’,’C3’,‘2020-2’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T1’,’C4’,‘2021-2’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T6’,’C1’,‘2020-1’);
INSERT INTO TC(tno,cno,semester) VALUES(‘T6’,’C3’,NULL);
#二、 在图形化工具(SQLyog或Navicat)里打开此文件,按CTRL+F9执行所有的命令,生成完整的Teach数据库
– 若有Teach数据库则先删除它
DROP DATABASE IF EXISTS Teach;
– 创建Teach数据库
CREATE DATABASE IF NOT EXISTS Teach;
#show databases;
– 选择Teach数据库为当前数据库
USE teach;
– 创建department表
CREATE TABLE Department
(dno CHAR(2) PRIMARY KEY,
dname VARCHAR(30) UNIQUE,
office VARCHAR(4),
note TEXT
);
– 创建student表
CREATE TABLE Student
(sno CHAR(2) PRIMARY KEY,
sname VARCHAR(10) NOT NULL,
sex ENUM (‘男’,’女’) DEFAULT’男’ ,
birth DATE,
photo LONGBLOB,
hobby SET(‘阅读’,’游泳’,’登山’,’远足’),
dno CHAR(2),
CONSTRAINT fk_dno FOREIGN KEY(dno) REFERENCES Department(dno)
);
– 创建course表
CREATE TABLE Course
(cno CHAR(2),
cname VARCHAR(20),
cpno CHAR(2),
credit ENUM(‘1’,‘2’,‘3’,‘4’),
PRIMARY KEY(cno),
CONSTRAINT uq_cname UNIQUE(cname)
);
– 创建sc表
CREATE TABLE SC
(sno CHAR(2),
cno CHAR(2),
score SMALLINT,
CONSTRAINT ck_score CHECK(score BETWEEN 0 AND 100),
CONSTRAINT fk_sno FOREIGN KEY(sno) REFERENCES Student(sno),
/表级完速性约束,sno是表的外码,被参照表是student,被参照列是sno/
CONSTRAINT fk_cno FOREIGN KEY(cno) REFERENCES Course(cno),
/表级完速性约束,cno是表的外码,被参照表是course,被参照列是cno/
/外码约束的实现,表明sno和cno都是表sc的外码/
PRIMARY KEY(sno,cno) /主码约束的实现/
);
– 创建Teacher表
CREATE TABLE Teacher
(tno CHAR(2) PRIMARY KEY,
tname VARCHAR(10) NOT NULL,
prof VARCHAR(10),
engage DATE,
dno CHAR(2),
CONSTRAINT t_fk_dno FOREIGN KEY(dno) REFERENCES Department(dno)
);
– 创建TC表
CREATE TABLE TC
(tcid INT AUTO_INCREMENT PRIMARY KEY,
tno CHAR(2),
cno CHAR(2),
semester CHAR(6),
CONSTRAINT fk_tno FOREIGN KEY(tno) REFERENCES Teacher(tno),
CONSTRAINT tc_fk_cno FOREIGN KEY(cno) REFERENCES Course(cno)
);
INSERT INTO Department VALUES(‘D1’,’计算机学院’,’C101’,’成立于2001年’);
INSERT INTO Department VALUES(‘D2’,’软件学院’,’S201’,’成立于2011年’);
INSERT INTO Department VALUES(‘D3’,’网络学院’,’F301’,’成立于2019年’);
INSERT INTO Department VALUES(‘D4’,’工学院’,’B206’,’成立于2000年’);
INSERT INTO STUDENT(sno,sname,sex,birth,hobby,dno) VALUES(‘S1’,’张轩’,’男’,‘2001-7-21’,’阅读,游泳’,’D1’);
INSERT INTO STUDENT(sno,sname,sex,birth,hobby,dno) VALUES(‘S2’,’陈茹’,’女’,‘2000-4-16’,’游泳,远足’,’D3’);
INSERT INTO STUDENT(sno,sname,sex,birth,hobby,dno) VALUES(‘S3’,’于林’,’男’,‘2000-12-12’,’登山,远足’,’D3’);
INSERT INTO STUDENT(sno,sname,sex,birth,hobby,dno) VALUES(‘S4’,’贾哲’,’女’,‘2002-2-18’,’阅读,登山’,’D1’);
INSERT INTO STUDENT(sno,sname,sex,birth,hobby,dno) VALUES(‘S5’,’刘强’,’男’,‘2001-8-1’,’游泳,登山’,’D2’);
INSERT INTO STUDENT(sno,sname,sex,birth,hobby,dno) VALUES(‘S6’,’冯玉’,’女’,‘2000-10-9’,’阅读,远足’,’D4’);
INSERT INTO COURSE VALUES(‘C1’,’数据库’,’C3’,4);
INSERT INTO COURSE VALUES(‘C2’,’计算机基础’,NULL,3);
INSERT INTO COURSE VALUES(‘C3’,’C_Design’,’C2’,2);
INSERT INTO COURSE VALUES(‘C4’,’网络数据库’,’C1’,4);
ALTER TABLE course ADD CONSTRAINT fk_cpno FOREIGN KEY(cpno) REFERENCES course(cno);
INSERT INTO SC VALUES(‘S1’,’C1’,90);
INSERT INTO SC VALUES(‘S2’,’C2’,82);
INSERT INTO SC VALUES(‘S3’,’C1’,85);
INSERT INTO SC VALUES(‘S4’,’C1’,46);
INSERT INTO SC VALUES(‘S5’,’C2’,78);
INSERT INTO SC VALUES(‘S1’,’C2’,98);
INSERT INTO SC VALUES(‘S3’,’C2’,67);
INSERT INTO S