最快,不服来战
2024-06-27 17:22:35
发布于:上海
显然,这是一道模拟。
Obviously, this is an imitate question.
可是,这道题的题面有些坑。
Fyxj, there are a lot of tricks in the question.
我们不需要找到最优解,
Finding the best solution isn't our work,
我们只需要贪心地有券用券。
Else than using the tickets greedily.
运行,提交,AC,我很快就完成了这道题。
Run, Submit, Accepted, I finished this question quickly.
现在给上我的第一版代码。
My first edition of code is given now.
// Execute in 2023-09-21 20:31:30.
#include <stdio.h>
struct tic{
int t, m;
};
tic queue[114514];
int head=0, tail=0, tmptail=0;
int n, s=0, tmp;
int type, price, time__;
int main(){
scanf("%d", &n);
while(n--){
scanf("%d%d%d", &type, &price, &time__);
if(type==0){
s += price;
queue[tail].t = time__;
queue[tail].m = price;
tail++;
}
else{
tmp = 0;
tmptail = tail;
while(head<tmptail){
if(time__-queue[head].t<=45){
if(price<=queue[head].m){
head++;
tmp = 1;
break;
}
queue[tail++] = queue[head];
}
head++;
}
if(!tmp) s += price;
while(head<tmptail){
queue[tail++] = queue[head++];
}
}
if(head==tail) head=tail=0;
}
printf("%d", s);
return 0;
}
可是,它运行了 28ms。
However, it had run for 28ms.
考虑增加优化。
Addition of optimization be thought.
数组的长度太臭了,
Length of array is too awful,
最后我们把它缩短至 。
Finally, we shorten it into .
但是,这不能优化多少。
However, this can't optimize a lot.
嗯……对了,可以用快读!
Ummm... Yes, We can use "fast read"!
正常的快读是这样的:
Normal fast read is like this:
inline int read(){
int x=0;bool w=0;
char ch=gc();
while(ch<'0'||ch>'9'){
if(ch=='-') w=1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return w?-x:x;
}
读数字是读整数中最耗时的一步。
Digit reading is the most time-consuming method in interger reading.
最近,我在 OI wiki 上看到了一种使用 fread()
的快读。
Recently, I saw a kind of fast reading which using fread()
.
把许多次读数字合并成一次是个好主意。
Unite a lot of digit readings into one time is a good idea.
当~这就是最终代码:(3ms, 2.51MB, 行)
Dang~This is the final code: (3ms, 2.51MB, lines)
#include <cstdio>
using namespace std;
#define ll long long
#define ull unsigned long long
namespace io{
const int size=(1<<20)+1;
char buf[size],*p1=buf,*p2=buf;
int op1=-1;
const int op2=size-1;
inline char readchar() {
if(p1!=p2) {
return *p1++;
}
return p1==(p2=(p1=buf)+fread(buf,1,size-1,stdin))?EOF:*p1++;
}
#ifndef ONLINE_JUDGE
#define gc getchar
#else
#define gc readchar
#endif
#define pc putchar
#define el pc(10)
#define sp pc(32)
inline int readi(){
int x=0;bool w=0;
char ch=gc();
while(ch<'0'||ch>'9'){
if(ch==EOF) return EOF;
if(ch=='-') w=1;
ch=gc();
}
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch^48);
ch=gc();
}
return w?-x:x;
}
inline ll readl(){
ll x=0;bool w=0;
char ch=gc();
while(ch<'0'||ch>'9'){
if(ch=='-') w=1;
ch=gc();
}
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch^48);
ch=gc();
}
return w?-x:x;
}
inline ll readull(){
ll x=0;
char ch=gc();
while(ch>=32){
if(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48);
ch=gc();
}
if(ch==-1) x=-x;
return x;
}
inline void reads(char* s){
char ch=gc();
while(ch>32) *(s++)=ch,ch=gc();
*s=0;
}
inline void readline(char* s){
char ch=gc();
while(ch>=32) *(s++)=ch,ch=gc();
*s=0;
}
inline void clearline(){
char ch=gc();
while(ch>=32) ch=gc();
}
void write(int x){
if(x<0) pc('-'),x=-x;
if(x>9) write(x/10);
pc((x%10)^48);
}
void write(ll x){
if(x<0) pc('-'),x=-x;
if(x>9) write(x/10);
pc((x%10)^48);
}
void write(ull x){
if(x>9) write(x/10);
pc((x%10)^48);
}
void writes(const char* s){
const char*pt=s;
while(*pt!=0)pc(*pt++);
}
};
using namespace io;
struct tic{
int t, m;
};
tic queue[100001];
int head=0, tail=0, tmptail=0;
int n, s=0, tmp;
int type, price, time__;
int main(){
n=readi();
while(n--){
type=readi(),price=readi(),time__=readi();
if(type==0){
s += price;
queue[tail].t = time__;
queue[tail].m = price;
tail++;
}
else{
tmp = 0;
tmptail = tail;
while(head<tmptail){
if(time__-queue[head].t<=45){
if(price<=queue[head].m){
head++;
tmp = 1;
break;
}
queue[tail++] = queue[head];
}
head++;
}
if(!tmp) s += price;
while(head<tmptail){
queue[tail++] = queue[head++];
}
}
if(head==tail) head=tail=0;
}
write(s);
return 0;
}
! 一举拿下最优解。
! It got the best solution.
好了,你们猜我为什么会写英文?因为把所有加粗的字符(各段首字符)拼在一起,就是:
Offer me half hundred!
v 我 50!
FInished.
全部评论 2
最后一个单词写错了,应该是finished
2024-10-14 来自 广东
0d
2024-06-27 来自 上海
0
有帮助,赞一个