问题 E: Shortest Distance (20)

 1 #include <iostream>
 2 #include<cstring>
 3 #include<vector>
 4 #define maxn 100001
 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 6 using namespace std;
 7 int n;
 8 typedef struct Dnode{
 9     int s;
10     int e;
11     int d;
12 }Dnode;
13
14 Dnode Dset[maxn];
15 int p1,p2;
16 void solve(){
17     int sum1=0;
18     int min;
19     int s=p1;
20     int e=p2;
21     while(s!=e){
22         sum1+=Dset[s].d;
23         s=Dset[s].e;
24     }
25     min=sum1;
26     sum1=0;
27
28     while(s!=p1){
29         sum1+=Dset[s].d;
30         s=Dset[s].e;
31     }
32     if(min<sum1) cout<<min<<endl;
33     else  cout<<sum1<<endl;
34
35 }
36 int main(int argc, char** argv) {
37     int n;
38     int i;
39     int d;
40     int t;
41     int j;
42     int test;
43     Dnode pa;
44     while(cin>>n){
45         t=1;
46         for(i=1;i<=n;i++){
47             cin>>d;
48             j=i+1;
49             if(j>n) j%=n;
50             Dset[t].s=i;
51             Dset[t].e=j;
52             Dset[t].d=d;
53             t++;
54         }
55
56         cin>>test;
57         while(test--){
58             cin>>p1>>p2;
59             solve();
60         }
61
62     }
63     return 0;
64 }

时间超时代码

优化一下,还超时:

代码如下:

 1 #include <iostream>
 2 #include<cstring>
 3 #include<vector>
 4 #define maxn 100001
 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 6 using namespace std;
 7 int n;
 8 typedef struct Dnode{
 9     int s;
10     int e;
11     int d;
12 }Dnode;
13
14 Dnode Dset[maxn];
15 int p1,p2;
16 int total_d;
17 void solve(){
18     int sum1=0;
19     int min;
20     int s=p1;
21     int e=p2;
22     while(s!=e){
23         sum1+=Dset[s].d;
24         s=Dset[s].e;
25     }
26     min=sum1;
27     sum1=total_d-min;
28     if(min<sum1) cout<<min<<endl;
29     else  cout<<sum1<<endl;
30
31 }
32 int main(int argc, char** argv) {
33     int n;
34     int i;
35     int d;
36     int t;
37     int j;
38     int test;
39     Dnode pa;
40     while(cin>>n){
41         t=1;
42         total_d=0;
43         for(i=1;i<=n;i++){
44             cin>>d;
45             total_d+=d;
46             j=i+1;
47             if(j>n) j%=n;
48             Dset[t].s=i;
49             Dset[t].e=j;
50             Dset[t].d=d;
51             t++;
52         }
53
54         cin>>test;
55         while(test--){
56             cin>>p1>>p2;
57             solve();
58         }
59
60     }
61     return 0;
62 }

暂时还没想到哪可以优化,不过我估摸着,得预先把每个点的最小值弄出来,因为M最大为10^4,而N最大为10^6 M*N=10^10 严重超时!

如果我能预先把它算出来的话最大的复杂度为O(10^4)

原文地址:https://www.cnblogs.com/industrial-fd-2019/p/10623546.html

时间: 2024-11-08 19:16:33

问题 E: Shortest Distance (20)的相关文章

1046 Shortest Distance (20 分)

1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contains one test case. For

1046 Shortest Distance (20分)

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contains one test case. For each case, the first line con

1046 Shortest Distance (20 分)(简单模拟,前缀和)

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 注意:1.这是一个环,两点之间的距离有两种,一是顺时针,二是逆时针,求出其中的一个距离,另一个距离用这个圆的总距离减去得到,输出两个距离之间最小的那个 2.注意区分x,y的大小,有可能下标大的在前面. 3.用到了前缀和的思想 1 #include <algorithm> 2 #include <iostream> 3

【PAT甲级】1046 Shortest Distance (20 分)

题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序号,输出他们之间的最小距离. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int dis[100007],sum[100007];int main(){ ios::sync_with_stdi

pat 1046 Shortest Distance(20 分) (线段树)

1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contains one test case. For

PAT——甲级1046S:shortest Distance

这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input

[LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL

Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Write a query to find the shortest distance between two points in these points. | x | |-----| | -1 | | 0 | | 2 | The shortest distance is '1' obviously, w

[LeetCode&amp;Python] Problem 821. Shortest Distance to a Character

Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = "loveleetcode", C = 'e' Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0] Note: S string leng

[Solution] 821. Shortest Distance to a Character

Difficulty: Easy Problem Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = "loveleetcode", C = 'e' Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2,